Results 1 to 2 of 2
Thread: Whats wrong with my while loop?
- 11-16-2011, 07:29 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 4
- Rep Power
- 0
Whats wrong with my while loop?
the task is to let the user input item code, and quantity he wants until he inputs "XX" to terminate the looping.
but my code seems to stop at one loop.
Q: do i have to use an array? or is it a logic problem? ( i will have to use the string inputs in the later part)
ps: i do not want an answer, i just want to know what misconception i have.
System.out.println("ENTER CODE (XX to Stop):");
System.out.print("CODE: ");
String i=scan.nextLine();
while(!i.equals("XX"))
{
System.out.println("ENTER QUANTITY: ");
int y=scan.nextInt();
System.out.println("ENTER CODE :");
i=scan.nextLine();
}
- 11-16-2011, 08:25 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: Whats wrong with my while loop?
There's nothing wrong with your while loop; it's the nextInt() method that's acting up; that method only reads digits but if you type in a number, say 42, you type '4' '2' and <enter>. The nextInt() method leaves the <enter> character(s) in the input buffer so the following nextLine() method assumes that it just read an empty line. The idiom is to add a dummy readLine() call just after you have read a number to get rid of that <enter> character in the input buffer.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Whats wrong with my code
By zit1343 in forum New To JavaReplies: 9Last Post: 01-24-2011, 01:06 AM -
Whats wrong with my program?
By jwb4291 in forum New To JavaReplies: 7Last Post: 12-12-2010, 04:40 AM -
whats wrong
By atenv in forum New To JavaReplies: 6Last Post: 06-15-2010, 01:55 PM -
whats is wrong with this app??
By mrajan in forum New To JavaReplies: 4Last Post: 06-09-2010, 10:56 PM -
Database help... whats wrong?
By neosnokia in forum JDBCReplies: 4Last Post: 06-09-2009, 11:17 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks