Simple user input not working
A seemingly simple task has thrown up a strange error. Ive got a while loop which keeps adding assignments until the user decides they have finished. When i run it, it skips the module title section and just puts the module identifier on the same line, an example output below.
Enter new assignment details
****************************
Module Titile :> Module Identifier :> test
Assignment Titile :> test
Everything else prints out as it should, i dont understand why though as they are all the same . . .
Code is below,
Code:
boolean add = true;
while(add)
{
System.out.println("\nEnter new assignment details");
System.out.println("****************************");
System.out.print("Module Titile :> ");
String mTitle = in.nextLine();
System.out.print("Module Identifier :> ");
String mIdentifier = in.nextLine();
System.out.print("Assignment Titile :> ");
String aTitle = in.nextLine();
System.out.print("Date Set :> ");
String dateSet = in.nextLine();
System.out.print("Due Date :> ");
String dueDate = in.nextLine();
System.out.print("Assignment Author :> ");
String aAuthor = in.nextLine();
System.out.print("Weighting :> ");
String weighting = in.nextLine();
System.out.println("\nEnter another assignment? (y/n)");
String another = in.next();
if(another.equals("n"))
add = false;
}
Re: Simple user input not working
Did you make sure "in" is empty at the beginning? Initialize or clear it and show us the declaration please.
Re: Simple user input not working
Is there anything else happening before this?
For example, is there a "Please choose your action" followed by some integer input?
Re: Simple user input not working
Quote:
Originally Posted by
Tolls
Is there anything else happening before this?
For example, is there a "Please choose your action" followed by some integer input?
Its a console menu so before this there is a series of options and then the scanner reads the input as an int value. That then brings you to this menu to add an assignment.
Re: Simple user input not working
I don't like being ignored... :=(:
Re: Simple user input not working
Sorry :)-:
The scanner is declared at the start of the class Scanner in = new Scanner(System.in);
It is then used in createMenu() to read an int as i said previously. I then use the scanner again in the above code to read a number of strings.
Ive never had to clear the scanner before using it again in similar programs, how would i do that?
Re: Simple user input not working
I would say
while (in.hasNext()) in.next();
But this is purely theoretical as I never worked with the scanner before, so I do not know if that is a blocking operation for the user if using a keyboard as input. :)
Re: Simple user input not working
Quote:
Originally Posted by
wdh321
Its a console menu so before this there is a series of options and then the scanner reads the input as an int value. That then brings you to this menu to add an assignment.
Scanner.nextInt() will result in the newline character being left in the buffer.
This character is then read when you use nextLine(), resulting in an empty String for that read.
This is what is happening here.
Stick a call to nextLine() prior to running this bit of code here (probably just after you've finished with the previous menu would be a good spot). This will have the effect Sierra talks about in their first post of clearing the buffer.
Re: Simple user input not working
Quote:
Originally Posted by
Tolls
Scanner.nextInt() will result in the newline character being left in the buffer.
This character is then read when you use nextLine(), resulting in an empty String for that read.
This is what is happening here.
Stick a call to nextLine() prior to running this bit of code here (probably just after you've finished with the previous menu would be a good spot). This will have the effect Sierra talks about in their first post of clearing the buffer.
Thank you, put a call to nextLine() and problem is solved.
Re: Simple user input not working
All the nextXXX() methods clear the buffer before reading except nextLine().
I have no idea at all why.
It's the cause of a large number of questions on this forum...:)