1

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.

1
  • Your mistake in if statements input.next().equals("y") || input.next().equals("yes") ... For break you program you should type in console n n n. If you type in console n after your first check input.next().equals("y") scanner will be empty - that why you not came to the second check equals("yes") and second if-else. Commented Sep 17, 2020 at 13:45

1 Answer 1

1

You need to store the value of input.next() in a variable before your if condition.

Something like this:

String next = input.next();

And replace all subsequent input.next() with next.


Explanation:

The reason it's not working is that with each input.next() it waits for a new string input.

Let's say you input: "n"

In the first if condition it will be compared with "y" which it won't match, the next input.next() will try to retake input, that's why it isn't showing anything because it's waiting for your input.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.