4 Replies - 5117 Views - Last Post: 09 July 2008 - 06:20 AM Rate Topic: -----

#1 vangogh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 30-May 08

User Input help/exception handling

Posted 08 July 2008 - 01:37 PM

I am trying to write a little program that allows a user to enter an int, and if they enter anything other than an int, they have to keep trying until an int is entered. Below is what I have so far. The problem is that when the user enters something other than an int, the program goes into an endless loop. I'm not too familiar with exception handling, so if someone can help, that's be great.

my code:
import java.util.Scanner;
import java.util.InputMismatchException;

public class InputTester
{
	static Scanner keyboard = new Scanner(System.in);
	static int number;
	static boolean tryAgain = true;
	
	public static void main(String[] args)
	{
		while(tryAgain)
		{
			try
			{
				System.out.println("Please enter an integer:");
				number = keyboard.nextInt();
				
				System.out.println("The number you entered was: "+number);
				tryAgain = false;
			}
			catch(InputMismatchException ime)
			{
				System.err.println("Input error.  Must enter an integer.  Try Again.");
			}
		}
	}
}



this is what prints endlessly if the user enters a non-int:
Input error.  Must enter an integer.  Try Again.
Please enter an integer:
Input error.  Must enter an integer.  Try Again.
Please enter an integer:



Is This A Good Question/Topic? 0
  • +

Replies To: User Input help/exception handling

#2 JeroenFM  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 18
  • View blog
  • Posts: 195
  • Joined: 30-June 08

Re: User Input help/exception handling

Posted 08 July 2008 - 02:41 PM

View Postvangogh, on 8 Jul, 2008 - 01:37 PM, said:

I am trying to write a little program that allows a user to enter an int, and if they enter anything other than an int, they have to keep trying until an int is entered. Below is what I have so far. The problem is that when the user enters something other than an int, the program goes into an endless loop. I'm not too familiar with exception handling, so if someone can help, that's be great.

my code:
import java.util.Scanner;
import java.util.InputMismatchException;

public class InputTester
{
	static Scanner keyboard = new Scanner(System.in);
	static int number;
	static boolean tryAgain = true;
	
	public static void main(String[] args)
	{
		while(tryAgain)
		{
			try
			{
				System.out.println("Please enter an integer:");
				number = keyboard.nextInt();
				
				System.out.println("The number you entered was: "+number);
				tryAgain = false;
			}
			catch(InputMismatchException ime)
			{
				System.err.println("Input error.  Must enter an integer.  Try Again.");
			}
		}
	}
}



this is what prints endlessly if the user enters a non-int:
Input error.  Must enter an integer.  Try Again.
Please enter an integer:
Input error.  Must enter an integer.  Try Again.
Please enter an integer:



While I am not familiar with the internals of the Scanner class it seems clear to me that it only asks for user input exactly once (just tested it). You can get a single String from the user and then parse it for multiple integers (or something else depending on the regular expression you use)

A simple way to solve it is to create a new scanner every time you restart the loop. Or you can get stuff from System.in manually by using the java.io package (for instance: wrapping it with a BufferedReader), performing readLine manually, and using java.lang.Integer.parseInt() to look for your integer
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: User Input help/exception handling

Posted 08 July 2008 - 05:08 PM

View PostJeroenFM, on 8 Jul, 2008 - 02:41 PM, said:

While I am not familiar with the internals of the Scanner class it seems clear to me that it only asks for user input exactly once (just tested it). You can get a single String from the user and then parse it for multiple integers (or something else depending on the regular expression you use)

A simple way to solve it is to create a new scanner every time you restart the loop. Or you can get stuff from System.in manually by using the java.io package (for instance: wrapping it with a BufferedReader), performing readLine manually, and using java.lang.Integer.parseInt() to look for your integer

Sorry JeroenFM, with all the respect that I have for you this is really not a good idea...
You do not want to create multiple instances of Scanner reading from the same InputStream... you will never know which one will receive the next input.
The facts are that if you have garbage (not an int) in your InputStream it is not read by your Scanner.nextInt() so you have to "flush" it before trying to read another int. So:

catch(InputMismatchException ime)
{
				System.err.println("Input error.  Must enter an integer.  Try Again.");
				keyboard.nextLine();   // flush the String that is not an int
 }


Was This Post Helpful? 0
  • +
  • -

#4 vangogh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 30-May 08

Re: User Input help/exception handling

Posted 08 July 2008 - 10:18 PM

Thanks pbl, works great.
Was This Post Helpful? 0
  • +
  • -

#5 JeroenFM  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 18
  • View blog
  • Posts: 195
  • Joined: 30-June 08

Re: User Input help/exception handling

Posted 09 July 2008 - 06:20 AM

View Postpbl, on 8 Jul, 2008 - 05:08 PM, said:

View PostJeroenFM, on 8 Jul, 2008 - 02:41 PM, said:

While I am not familiar with the internals of the Scanner class it seems clear to me that it only asks for user input exactly once (just tested it). You can get a single String from the user and then parse it for multiple integers (or something else depending on the regular expression you use)

A simple way to solve it is to create a new scanner every time you restart the loop. Or you can get stuff from System.in manually by using the java.io package (for instance: wrapping it with a BufferedReader), performing readLine manually, and using java.lang.Integer.parseInt() to look for your integer

Sorry JeroenFM, with all the respect that I have for you this is really not a good idea...
You do not want to create multiple instances of Scanner reading from the same InputStream... you will never know which one will receive the next input.
The facts are that if you have garbage (not an int) in your InputStream it is not read by your Scanner.nextInt() so you have to "flush" it before trying to read another int. So:

catch(InputMismatchException ime)
{
				System.err.println("Input error.  Must enter an integer.  Try Again.");
				keyboard.nextLine();   // flush the String that is not an int
 }



I never said it was a good solution :P That's what you get for answering questions on things you never used before I guess :crazy:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1