Whenever I run the following code, the if statement that asks for yes/y works when run, however, the else if and else statements don't work. When I try typing in n, no, or anything else, it just stays there. No reaction, no crashes, just sticks there. Anyways here's the code:
public static void main(String[] args) {
String[] items;
double[] cost;
int[] quantity;
double[] totalItemCost;
int amountOfItems;
double totalCost = 0;
Scanner input = new Scanner(System.in);
while(true) {
try {
Thread.sleep(500);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
System.out.println("Welcome to The TC corner market, this is an automated cashier program to check out your items.\n");
System.out.println("May we proceed? y/n");
if (input.next().equals("y") || input.next().equals("yes")) {
try {
Thread.sleep(500);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
System.out.println("Okay, let's proceed.");
try {
Thread.sleep(200);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
System.out.println("How many items will you be checking out?");
amountOfItems = input.nextInt();
items = new String[amountOfItems];
cost = new double[amountOfItems];
quantity = new int[amountOfItems];
totalItemCost = new double[amountOfItems];
try {
Thread.sleep(200);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
for (int i = 0; i < amountOfItems; i++) {
System.out.println("\nWhat is the name of the item?");
items[i] = input.next();
System.out.println("What is the cost of the item?");
cost[i] = input.nextDouble();
System.out.println("What is the quantity of the item you are purchasing?");
quantity[i] = input.nextInt();
totalItemCost[i] = cost[i] * quantity[i];
System.out.printf("Item:\t\tCost:\t\tQuantity:\t\tTotal Item Cost:\n%s\t\t%.2f\t\t%d\t\t\t\t%.2f", items[i], cost[i], quantity[i], totalItemCost[i]);
totalCost = totalCost + cost[i];
}
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
System.out.println("\n\nYour Checkout Cart:\n");
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
System.out.println("Items:\t\tCost:\t\tQuantity:\t\tTotal Item Cost:\n");
for (int j = 0; j < amountOfItems; j++) {
System.out.printf("%s\t\t%.2f\t\t%d\t\t\t\t%.2f\n", items[j], cost[j], quantity[j], totalItemCost[j]);
}
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
System.out.printf("\nYour total: %.2f", totalCost);
try {
Thread.sleep(3000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
System.out.println("\n\nThank you for checking out with The TC corner market! This program will restart in 10 seconds.");
try {
Thread.sleep(10000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
} else if (input.next().equals("n") || input.next().equals("no")) {
System.out.println("In that case, please proceed to return your items back to the store");
try {
Thread.sleep(3000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
break;
} else {
try {
Thread.sleep(200);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
System.out.println("Invalid Answer");
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
break;
}
}
}
I tried doing many things, like changing it from else if to regular if, adding the break (which I now have in my code I am posting above), and just completely removing the else statement and turning the else if to else. None of them worked though.
input.next().equals("y") || input.next().equals("yes")... For break you program you should type in consolen n n. If you type in consolenafter your first checkinput.next().equals("y")scanner will be empty - that why you not came to the second checkequals("yes")and second if-else.