Jump to content

Scanner problems

- - - - -

  • Please log in to reply
4 replies to this topic

#1
travy92

travy92

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
Hey everyone, I'm having trouble getting the scanner to work for user input.
Basically what's happening is if I input something other than an integer for:

Age = inputVariable.nextInt();

It will give me this error:
java.util.InputMismatchException
.

This is what I have so far:
try { 

		// Get Age

		System.out.println("Age: ");

		Age = inputVariable.nextInt();

		} catch (Exception e) {

			Age = 0;

			inputVariable.reset();

			System.out.println("You have not entered a valid age, please try again.");

                        Age = inputVariable.nextInt();

			System.out.println("Age: ");

       }

I've added Age=0 and inputVariable.reset to try and clear it for re-input but it does not work. I just receive the same error once it gets to the second user input for Age.

So can someone tell me how to clear the scanner for another input?

Thanks.
[SIGPIC]C:\Users\Travis\Desktop\Image Converter\Knight1.bmp[/SIGPIC]

#2
Simonxz

Simonxz

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts
You can use the method hasNextInt() to know if the user entered an int. Like so :


import java.util.Scanner;

public class Bleh {

	public static void main(String args[]) {

		Scanner inputVariable = new Scanner(System.in);

		int age = 0;

		System.out.print("Age: ");

		if (inputVariable.hasNextInt())

			age = inputVariable.nextInt();

		else 

			System.out.println("You have not entered a valid age, please try again.");


		System.out.println("Age " + age);		

	}

}


If you want to loop until the user input a integer, use a while loop. Like that


import java.util.Scanner;

public class Bleh {

	public static void main(String args[]) {

		Scanner inputVariable = new Scanner(System.in);

		int age = 0;

		while (age == 0) {

			System.out.print("Age: ");

			if (inputVariable.hasNextInt())

				age = inputVariable.nextInt();

			else {

				inputVariable.nextLine();

				System.out.println("You have not entered a valid age, please try again.");

			}

		}

		System.out.println("Age " + age);

	}

}


Edited by Simonxz, 13 March 2011 - 10:57 AM.


#3
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
You propably enter after inputting the integer. That enter is still in the queue. Do nextLine() to get rid of the "\n" (and anything else left :p)

#4
travy92

travy92

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
Thanks for the help, this solved the problem. However I have encountered another problem. If the user presses the Return keys (enter) a few times, and then types something 5 lines down, the program will then return 5 messages saying the user has not inputted a valid number.
Eg.







~somthing invalid~

Will return:
You have not entered a valid age, please try again.

You have not entered a valid age, please try again.

You have not entered a valid age, please try again.

You have not entered a valid age, please try again.

You have not entered a valid age, please try again.

[SIGPIC]C:\Users\Travis\Desktop\Image Converter\Knight1.bmp[/SIGPIC]

#5
arunjib

arunjib

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
its a useful post for me




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users