-
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
-
Not sure I understand your question. Can you post your code?
-
 Originally Posted by destin
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!
-
so here is the code in a shortened version
Code:
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
-
 Originally Posted by tbrigham
Code:
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.
Code:
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
}
}
}
-
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
-
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.
Similar Threads
-
Replies: 5
Last Post: 02-17-2006, 09:12 AM
-
By fatboy in forum VB Classic
Replies: 2
Last Post: 01-19-2006, 08:54 AM
-
By william in forum Database
Replies: 0
Last Post: 02-05-2002, 03:11 PM
-
Replies: 3
Last Post: 08-30-2001, 11:45 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks