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

Thread: Scanner not working properly.

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Scanner not working properly.

    System.out.print("Enter the student's name: ");
    name = input.nextLine();
    System.out.print("Enter the id: (exactly 8 digits): ");
    id = input.nextInt();
    if ((id / LOGIN_ID_DIGITS) > 10 || (id / LOGIN_ID_DIGITS) < 1)
    {
    System.out.println("***You did not enter an 8 digit id.\n");
    continue;
    }
    System.out.print("Enter the address: ");
    address = input.nextLine();
    Student s = new Student(name, id, address, 0, 0.0);

    My program will not let me enter string input. It reads the input as ("Enter the id: (exactly 8 digits): "). The integer input is fine.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,210
    Thanks
    52
    Thanked 2,294 Times in 2,265 Posts

    Default Re: Scanner not working properly.

    My program will not let me enter string input.
    Please explain. Post the full contents of the console showing what happened.

    What method is trying to read what you are entering?
    nextInt() only accepts numeric digits.

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

    Default Re: Scanner not working properly.

    College directory commands:
    add - add a new student
    find - find a specific student
    addCourse - add a course's grade points for a specific student
    login - get a specific student's login id
    highest - find a student with the highest GPA in the college
    quit - quit
    Enter a command: add

    Enter the student's name: Enter the id: (exactly 8 digits): 12345678
    Enter the address: College directory commands:


    The college directory commands will continue to loop until the user enters quit.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,210
    Thanks
    52
    Thanked 2,294 Times in 2,265 Posts

    Default Re: Scanner not working properly.

    Can you show what the problem is? Add some comments to what you posted to show where the problem is.

    I suspect the problem is with the newline character being left in the Scanner class's buffer after calling nextInt()
    You must call nextLine() to clear the newline character from Scanner's buffer.

  5. #5
    Junior Member
    Join Date
    Jan 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Scanner not working properly.

    System.out.print("Enter the student's name: ");
    name = input.nextLine(); //this input is read as the print statement below.
    System.out.print("Enter the id: (exactly 8 digits): ");
    id = input.nextInt(); //user can enter an integer
    if ((id / LOGIN_ID_DIGITS) > 10 || (id / LOGIN_ID_DIGITS) < 1)
    {
    System.out.println("***You did not enter an 8 digit id.\n");
    continue;
    }
    System.out.print("Enter the address: ");
    address = input.nextLine(); //and again the input is read as the next print statement
    Student s = new Student(name, id, address, 0, 0.0);

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,210
    Thanks
    52
    Thanked 2,294 Times in 2,265 Posts

    Default Re: Scanner not working properly.

    Does it work now?

  7. #7
    Junior Member
    Join Date
    Jan 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Scanner not working properly.

    No I haven't found a solution.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,210
    Thanks
    52
    Thanked 2,294 Times in 2,265 Posts

    Default Re: Scanner not working properly.

    From post #4
    I suspect the problem is with the newline character being left in the Scanner class's buffer after calling nextInt()
    You must call nextLine() to clear the newline character from Scanner's buffer.

  9. #9
    Junior Member
    Join Date
    Jan 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Scanner not working properly.

    I rearranged the order of input by asking for the name, address, then student id. But I still get a problem where I can't input the student's name!
    So I copy and pasted this to a new blank class and it works. How come it does not work properly in my program!

    The problem is - Enter the student's name: Enter the address: It won't let me type the input for student's name, it goes straight to asking for the address.

    Is there a way to show you guys my program...this is frustrating!!!

    It should look like this (from the new class I said I used to test):

    Enter the student's name: Bob Dylan
    Enter the address: 123 Tebow Street
    Enter the id: (exactly 8 digits): 12345678
    Last edited by kookevk; January 25th, 2012 at 09:14 PM.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,210
    Thanks
    52
    Thanked 2,294 Times in 2,265 Posts

    Default Re: Scanner not working properly.

    Did you try what I recommended earlier:
    You must call nextLine() to clear the newline character from Scanner's buffer.
    nextInt leaves the newline in the buffer.
    Call nextLine to clear it.

Similar Threads

  1. not looping properly, please help.
    By nakedtriple in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 28th, 2011, 08:31 AM
  2. Complete but not working properly
    By Noob101 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 12th, 2011, 02:17 PM
  3. I cannot get boolean to work properly
    By kl2eativ in forum What's Wrong With My Code?
    Replies: 9
    Last Post: January 18th, 2011, 06:37 AM
  4. if else statement not working properly
    By tina G in forum Algorithms & Recursion
    Replies: 1
    Last Post: March 29th, 2010, 08:04 AM
  5. Replies: 4
    Last Post: January 26th, 2009, 11:03 PM