Skipped User Input
this may seem like a trivial matter but i am executing a while loop with a System.out.println(...); command followed by a call to a scanner to recieve input from the user. this executes perfectly the first time but after that the loop will execute once with out calling the scanner's .readLine() method and then going through again and calling it.
very frustrating and i have debugged and watched this happen and am very stumped as to why it only does it every other time...
any help or questions are appreciated
[539 byte] By [
tbrigham] at [2007-11-11 8:09:03]

# 1 Re: Skipped User Input
Not sure I understand your question. Can you post your code?
destin at 2007-11-11 22:36:24 >

# 2 Re: Skipped User Input
Not sure I understand your question. Can you post your code?
sorry i wasnt clear enough... when i get back to my room i will post a shortened version of the method this error occurs in. thanks for the followup though!
# 3 Re: Skipped User Input
so here is the code in a shortened version
private License editLicense(License licenseToEdit, Scanner r) {
while(true) {
System.out.print("\n Please enter a command: (E)dit name, (A)dd a ticket, (D)elete a ticket, (R)eturn to previous level: ");
String tempCommand = r.nextLine();
if (tempCommand.equalsIgnoreCase("r")) {
... do something else
}
else if (tempCommand.equalsIgnoreCase("e")) {
... do something else
}
else if (tempCommand.equalsIgnoreCase("a")) {
... so something else
}
else if (tempCommand.equalsIgnoreCase("d")) {
... do something else
}
}
}
the problem is is that the first print (the one with "Enter a command...") prints out twice for each input scanned by the scanner. meaning the scanner only gets input from the user once every two times... i have no idea why
# 4 Re: Skipped User Input
i have actually found a fix for this. it appears that it when a scanners .nextDouble() method is invoke it for some reason skips the next call to the .nextLine() method. dont ask me why... cause i dont know. maybe some of you know? but i countered this by adding a call to the .nextLine() method right after the .nextDouble() and it skips this one and gives me correct output!
this is still not perfect because it is not logical and does not make sense and i do not like having random method calls that do not do anything. so for now it is a fix but i am still looking into the root cause of this problem.
oh and this works fine with a BufferedReader but my teach wants a single scanner so he can use some input textfiles on my program. so the BufferedReaders are out of the question unless someone knows an easy way to parse ints and doubles from a BufferedReader (or any other type of reader besides a Scanner)
thanks
# 5 Re: Skipped User Input
UPDATE (for those that care):
i have recently trashed the idea of using a Scanner and have gone back to using a BufferedReader. now to parse the double input from the string made with the .readLine() method i am now using the method .parseDouble(String); in the Double class. this works well and am happy with the results.
# 6 Re: Skipped User Input
private License editLicense(License licenseToEdit, Scanner r) {
while(true) {
System.out.print("\n Please enter a command: (E)dit name, (A)dd a ticket, (D)elete a ticket, (R)eturn to previous level: ");
String tempCommand = r.nextLine();
if (tempCommand.equalsIgnoreCase("r")) {
... do something else
}
else if (tempCommand.equalsIgnoreCase("e")) {
... do something else
}
else if (tempCommand.equalsIgnoreCase("a")) {
... so something else
}
else if (tempCommand.equalsIgnoreCase("d")) {
... do something else
}
}
}
If you're just checking for one char, just take the first character of the String.
private License editLicense(License licenseToEdit, Scanner scanner) {
while (true) {
System.out.print("\n\tPlease enter a command: (E)dit name, (A)dd a ticket, (D)elete a ticket, (R)eturn to previous level: ");
char c = scanner.nextLine().charAt(0);
if (Character.toLowerCase(c) == 'e') {
// edit name
} else if (Character.toLowerCase(c) == 'a') {
// add a ticket
} else if (Character.toLowerCase(c) == 'd') {
// delete a ticket
} else if (Character.toLowerCase(c) == 'r') {
// return to previous level
}
}
}
destin at 2007-11-11 22:41:24 >

