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 5 of 5

Thread: Scanner skipping input

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Scanner skipping input

    I am (obviously) a beginner. For the life of me I can't figure out why my scanner is skipping - not giving me the opportunity to input the Strings, it does however let me input the ints (when I choose option 1). Here is my code:

    //--------------------------------------------------------------------------------
    import java.util.Scanner;

    public class InvoiceDemo {

    public static void main(String[] args) {

    // VARIABLES ----------------------------------------------------
    int number, id, hours;
    String name, address;

    // --------------------------------------------------------------
    // Create scanner
    Scanner keyboard = new Scanner(System.in);

    // Prompt user for selection - loop until valid input is received
    do {
    System.out.println("Please make a selection [1 - Add Client / 2 - Create Invoice / 3 - Quit]: ");
    number = keyboard.nextInt();
    } while (number < 1 || number > 3);

    if (number == 1) {
    System.out.print("Please enter the client's name: ");
    name = keyboard.nextLine();
    System.out.print("Please enter the client's numerical ID: ");
    id = keyboard.nextInt();
    System.out.print("Please enter the client's address: ");
    address = keyboard.nextLine();
    System.out.print("Please enter the number of hours worked: ");
    hours = keyboard.nextInt();
    }
    else if (number == 2) {
    System.out.println("Creating Invoice...");
    }
    else
    System.exit(0);

    }

    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,290
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: Scanner skipping input

    What do you mean when you say it's not letting you? Do you receive an Exception, or does something else happen? Have you read through the API for the Scanner class? Namely, do you understand the difference between nextInt() and nextLine()? Where is the Scanner after each method is called?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Scanner skipping input

    When I run the program, this is what I get:

    Please make a selection [1 - Add Client / 2 - Create Invoice / 3 - Quit]:
    1
    Please enter the client's name: Please enter the client's numerical ID: 123
    Please enter the client's address: Please enter the number of hours worked: 40

    I have read the Scanner class info, but don't see what I'm doing wrong. I believe NextInt() returns the next integer entered in the scanner object and NextLine() returns the next entry in the scanner (including spaces) as a String. I'm not sure what you mean by where is the Scanner after each method is called. My scanner is called "keyboard" so when I write keyboard.nextInt() it calls the scanner object right?

    Process completed.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,290
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: Scanner skipping input

    Pay close attention to when the new line is passed by the Scanner. With nextInt(), you won't pass over the new line (which you add when you hit enter after typing an int). Then when you call nextLine(), the new line is still there, so it thinks that you want the empty String that was "entered" between the int and the new line.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: Scanner skipping input

    I'm not sure what you mean by where is the Scanner after each method is called.
    The scanner works with a stream of characters (characters you can see, and characters like spaces and newlines). Every time you call nextWhatever() something is returned and the position of the scanner within the stream is updated. The documentation is very explicit (but maybe not clear...) about where in the stream each call to nextXXX() leaves the scanner.

    And this makes a big difference. Your "address = keyboard.nextLine();" will do exactly what it is documented to do: including possibly returning an empty string straight away. Nothing in the documentation says it must wait if there is already a new line in the stream.

Similar Threads

  1. Replies: 5
    Last Post: October 19th, 2011, 08:21 PM
  2. Reading user input from the console with the Scanner class
    By JavaPF in forum Java SE API Tutorials
    Replies: 3
    Last Post: September 7th, 2011, 03:09 AM
  3. Skipping Input Problem
    By Matty Alan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 15th, 2011, 07:45 AM
  4. [SOLVED] if else skipping
    By Scotty in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 25th, 2010, 08:04 PM
  5. Reading user input from the console with the Scanner class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: November 11th, 2008, 01:47 PM