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.
Re: Scanner not working properly.
Quote:
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.
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.
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.
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);
Re: Scanner not working properly.
Re: Scanner not working properly.
No I haven't found a solution.
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.
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
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.