Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Loop using Scanner with try/catch

  1. #1
    Junior Member
    Join Date
    May 2019
    Location
    Right behind you
    Posts
    2
    My Mood
    Angelic
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Loop using Scanner with try/catch

    Hello all

    I haven't used Java in years been pursuing a Black Belt in a traditional martial art and received an injury and have had a 'lot' of free time recently so I decided to play around with some Java programming again and it's changed a lot.

    When I last used Java receiving user input wasn't handled with a scanner and there were no try/catch options. So I have used these things trying to get user input, and make sure a user enters a number, if not create a loop to go back and ask the user to enter again.

    Now, if the user enters a number all good, but, I just can't figure why the code goes into an infinite loop if the user enters anything else. It doesn't stop and ask the user to enter a number again?!? It just repeats the catch message. Any help would be greatly appreciated.

    And I used the following code: excuse the formatting I am coding in notepad and its not exactly the most readable:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
     
    import java.util.Scanner;
     
    //needed for the InputMismatchError
    import java.util.* ;
     
    public class First {
     
        public static void main(String args[]) {
            System.out.println(" You have just run HelloWorld !");
    	//BufferedReader br = new BufferedReader();
    	Scanner numberScan=new Scanner(System.in);
     
    	System.out.println("\nEnter a number");
     
    	//create system to ask for user input if entry is wrong
    	boolean entryWrong = true;
     
    	//get user input
    	//this is totally weird;
    	int userInt;
     
    	//try/catch pairing exception error if user doesn't enter a number,ie int
    	//in this case
     
    	//while(userEntry==true){
    	while(true){
    	try{
    		userInt = numberScan.nextInt();
    		System.out.println("You Entered a number: "+ userInt);	
    		break;
    		//userEntry = false;
    		}
    	catch (InputMismatchException e){
    		System.out.print("\nPlease enter a number");
    		//userEntry = true;
    	}//end try catch
     
    	}//end while repeat
     
    	System.out.println("End Main");
        }//end main
     
     
    }//end class
    Last edited by CelticWarrior; May 30th, 2019 at 10:26 PM.

  2. The Following User Says Thank You to CelticWarrior For This Useful Post:

    loncillosamm (July 6th, 2019)

  3. #2
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    262
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Default Re: Loop using Scanner with try/catch

    You need to add numberScan.next(); after line System.out.print("\nPlease enter a number"); in catch block. You can find more info from here https://stackoverflow.com/questions/...ext-or-nextfoo
    Whatever you are, be a good one

  4. #3
    Junior Member
    Join Date
    May 2019
    Location
    Right behind you
    Posts
    2
    My Mood
    Angelic
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Loop using Scanner with try/catch

    Quote Originally Posted by John Joe View Post
    You need to add numberScan.next(); after line System.out.print("\nPlease enter a number"); in catch block. You can find more info from here https://stackoverflow.com/questions/...ext-or-nextfoo
    That simple huh!
    facepalm.gif

  5. #4
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    262
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Default Re: Loop using Scanner with try/catch

    Is a common mistake made by most of the beginner
    Whatever you are, be a good one

Similar Threads

  1. [SOLVED] Try... catch loop?!
    By deeevo in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 10th, 2013, 05:36 PM
  2. Using for loop to read in data from text file using Scanner class
    By Scippi in forum Loops & Control Statements
    Replies: 23
    Last Post: February 26th, 2013, 10:53 PM
  3. Scanner (death loop)
    By F6e9a in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 11th, 2012, 10:28 PM
  4. [SOLVED] While try catch loop
    By Duaber in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 23rd, 2011, 02:14 PM
  5. Jumps over one scanner in if else loop
    By MikalD in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 5th, 2011, 08:34 AM