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();
}
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,
Jos