I have tried the following code in many ways but it just doesn’t work. I have two problems with it.
- I need it to not continue to QUANTITY when I press X.
- If I want to continue, i.e., I do not press X but enter a code I should be pressing, it takes the first input correctly, however, when it goes around the loop the second type, it outputs something like “CODE: QUANTITY: ” on the same line.
I would really appreciate help here, because I am stuck and google’s been no help. I am completely new to programming, with no previous experience in any language, so I would really appreciate detailed help.
Here’s the code:
import java.util.Scanner;
class WHY
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
boolean count = true;
for (int i = 0; count; i++)
{
System.out.print("CODE: (X to terminate)");
String code = in.nextLine();
System.out.print("QUANTITY: ");
int quantity = in.nextInt();
if (code.equals("X"))
count = false;
}
}
}
1 Answer