My apologies that I overlooked the do.
The problem is the outer loop while(cha != 'q').
And also the condition while(cha2 == 1) should actually be while(cha2 != 1)
Here is a little fix just for demonstration:
Scanner in = new Scanner(System.in);
int cha1, cha2 = 0, fruits = 10, veg = 15, toy = 15, groc = 35, cha, am1 = 0;
System.out
.println("\n\n\n\n\nTo Add more Items:1\n\nTo View Existing Items:2");
cha = in.nextInt();
while (cha != 'q') {
if (cha == 1)
do {
System.out.println("\n\n\n\nEnter amount of:");
System.out
.println("\nFruits:1\nVegetables:2\nToys:3\nGroceries:4");
cha1 = in.nextInt();
if (cha1 == 1) {
System.out.println("Fruits=" + fruits
+ "\n\nEnter Amount:");
fruits += (am1 = in.nextInt());
System.out.println("New Amount:" + fruits);
} else if (cha1 == 2) {
System.out.println("Vegetables=" + veg
+ "\n\nEnter Amount:");
veg += (am1 = in.nextInt());
System.out.println("New Amount:" + veg);
} else if (cha1 == 3) {
System.out.println("Toys=" + toy + "\n\nEnter Amount:");
toy += (am1 = in.nextInt());
System.out.println("New Amount:" + toy);
} else if (cha1 == 4) {
System.out.println("Groceries=" + groc
+ "\n\nEnter Amount:");
groc += (am1 = in.nextInt());
System.out.println("New Amount:" + groc);
} else {
System.out
.println("Wrong Input \n\n"
+ "Enter 1 To Terminate or any other digit to Continue?");
cha2 = in.nextInt();
if(cha2 == 1) {
cha = 'q';
}
}
} while (cha2 != 1);
}
This terminates correctly, because I set cha to 'q' if the user entered '1'
However, this is bad practice.
You shouldn't declare cha as int, if you actually save and test for characters. Use the type char.
The use of in.nextInt() will crash the program if the user enters a character. The correct approach is to read the line into a String instead.