Results 1 to 2 of 2
  1. #1
    beatzz is offline Member
    Join Date
    Mar 2009
    Posts
    5
    Rep Power
    0

    Default Understanding Scanner.nextLine(), next(), and nextInt() ?

    I'm experimenting with the nextLine(), next(), and nextInt() methods of the Scanner class. I am only finding nextLine() to work, on lines 20 & 23 you'll notice I have alternative methods to instantiate the variables 'name' & 'age'. When using these alternative methods, Line 20 results in a runtime error, and line 23 results in a logical error, it somehow disables the ability to assign a value to the 'rerunApp' String variable.

    Java Code:
    package example_project;
    
    import java.util.Scanner;
    import java.text.NumberFormat;
    
    public class Test {
    
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		String rerunApp = "y";
    		while (rerunApp.equalsIgnoreCase("y")) {
    			System.out.println("------------------------" + "\n" +
    				                    "Welcome to the Test app!" + "\n" +
    				                    "------------------------");
    			System.out.print("\n" + "Would you like to run the scanner code? (y/N) ");
    			String scannerBlock = sc.nextLine();
    			if (scannerBlock.equalsIgnoreCase("y")) {
    				System.out.print("Enter your name: ");
    				String name = sc.nextLine();
    //				String name = sc.next();
    				System.out.print("Enter your age: ");
    				int age = Integer.parseInt(sc.nextLine());
    //				int age = sc.nextInt();
    				System.out.println("\n" + "Hello " + name + ", you are " + age + " years old.");
    			}
    			System.out.print("\n" + "Run the Test App again? (y/N) ");
    			rerunApp = sc.nextLine();
    		}
    		sc.close();
    		System.out.print("\n" + "Goodbye!");
    	}
    }
    Any help understanding what is going wrong when using next() or nextInt() ?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    25

    Default Re: Understanding Scanner.nextLine(), next(), and nextInt() ?

    You might need to post the actual errors (with stacktraces) that you get.

    However, you need to understand how Scanner works...there's a gotcha tucked away.

    Scanner works based on delimiters for all its next methods apart from nextLine.
    That is, it reads up to (but not including) the next delimiter it encounters, returning the result, after skipping (throwing away) any leading delimiter.

    So, the default Scanner, which uses whitespace as delimiter will take an input like ' foo ' and a call to next() will throw away the leading space, and return foo.

    What often happens is people will use a Scanner like this ('sc' is a standard Scanner on System.in):
    Java Code:
    // get a number
    int someNumber = sc.nextInt();
    // get a line of text
    String someText = sc.nextLine();
    And then wonder why 'someText' is blank.
    It's blank because nextInt has left the newline in the buffer as it's whitespace and hence a delimiter.
    Then nextLine reads in that newline character as its line, and gives you the empty string.

    Indeed if the line after nextLine call was:
    Java Code:
    // get another number
    int anotherNumber = sc.nextInt();
    You would then likely get an exception as the value entered by the user for the someText variable would be still in the buffer, and probably wouldn't be a valid int.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Scanner's nextLine() won't let me enter anything
    By stupidmoron in forum New To Java
    Replies: 3
    Last Post: 02-04-2014, 08:38 AM
  2. Scanner.nextLine ??
    By NotTooOld in forum New To Java
    Replies: 5
    Last Post: 05-07-2013, 01:26 PM
  3. reading nextLine after a nextInt not working
    By Jadeclan in forum New To Java
    Replies: 2
    Last Post: 11-04-2011, 03:12 AM
  4. Replies: 1
    Last Post: 09-30-2011, 12:13 AM
  5. Reading nextInt() then nextLine() form a file.
    By Mokomi in forum New To Java
    Replies: 9
    Last Post: 09-06-2011, 08:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •