0
public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        boolean start = true;
        while(start)

                System.out.printf("%70s %n", " @@@@@     Zoos Australia     @@@@@ " + "\n");

                System.out.printf("%57s %n", "Main Menu" + "\n");

                System.out.printf("%72s %n", "Zoo has the following ticketing options:");

                System.out.print("\n");

                System.out.printf("%59s %n", "1 = Child (4-5 yrs)");
                System.out.printf("%59s %n", "2 = Adult (18+ yrs)");
                System.out.printf("%60s %n", "3 = Senior (60+ yrs)");

                System.out.println("\n");

                String choose1 = "";
                String choose2 = "";
                String choose3 = "";
                String selected = "";
                int option = 0;
                {
                    System.out.print("Please select an option: ");
                    option = input.nextInt();
                    if (option == 1) {
                        choose1 = "Child";
                        selected = choose1;
                    } else if (option == 2) {
                        choose2 = "Adult";
                        selected = choose2;
                    } else if (option == 3) {
                        choose3 = "Senior";
                        selected = choose3;
                    }
                }
                // done

                System.out.println("\n");


                int price = 0;
                int tickets = 0;
                System.out.print("Enter the number of tickets: ");
                tickets = input.nextInt();
                if (selected == choose1) {
                    price = 10;
                } else if (selected == choose2) {
                    price = 20;
                } else if (selected == choose3) {
                    price = 15;
                }


                System.out.println("\n");

                System.out.print("You are purchasing " + tickets + " " + selected + " tickets at " + "$" + price + " each!");

                System.out.println("\n");

                int confirm = 0;
                    System.out.print("Press 1 to confirm purchase: ");
                    confirm = input.nextInt();
                    if (confirm != 1) {
                        System.out.print("Incorrect Key. Please return to Main Menu");
                        System.out.println("\n");

                    } else {
                        break;
                    }


                System.out.println("\n");

                int total = tickets;
                price = total * price;
                System.out.print("Total amount for " + selected + " tickets: " + "$" + price);

                System.out.println("\n");

                String pick = "";
                System.out.print("Do you wish to continue: ");
                input.next();

                System.out.println("\n");
                if (pick == "no") {
                    System.out.print("Total amount payable is: " + "$" + price);
                    System.out.println("\n");
                    System.out.print("Have a nice day!");
                    System.out.println("\n");
                }}}

Trying to do this at the end of the program where user is asked "Do you wish to continue" using a method or something but cant get it to work. Either the program returns to main menu only or the program ends and displays the total message "Total amount payable..." etc. I have tried using while with continue and break. Using boolean with true and false. But no luck. Thank you anyone that may be able to clear this up for me please.

wallace
  • 1
  • 2
  • Does this help? [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – Abra Apr 24 '20 at 00:34
  • Quite possibly. I will give it a try. I didnt even think about that it was skipping the next line. Thanks. – wallace Apr 24 '20 at 00:47
  • Doesnt seem to be working... – wallace Apr 24 '20 at 00:54
  • Copied your code. It works for me. I enter `yes` when asked `Do you wish to continue` and the main menu is displayed again. – Abra Apr 24 '20 at 01:10
  • Thats true for me as well. But if the option is no it doesnt end, for me. – wallace Apr 24 '20 at 01:19

1 Answers1

0

First, you have to assign the users's input to a variable: pick = input.next(). After that, the problem is that you compare the user's input string with a "no" string by using == operator. When comparing reference types (objects) (and String is an object), in most cases == operator gives you an unpredictable result, because it compares the reference (address of an object in memory) and not the actual content. Please remember, that you always have to use the .equals() method instead. You also have to break from your loop, when the user's input is "no".

There is plenty of material concerning this issue. You can check, for instance, this one How do I compare strings in Java?

P.S. I quickly looked at the rest of your code and put some additional comments, which might help you to improve it. Good luck with learning Java!

    Scanner input = new Scanner(System.in);
    // boolean start = true; you don't need this line
    while(true) { // 'true' condition makes it an infinite loop until you use break
                  // You also have to surround your while loop with curly braces, 
                  // otherwise you fall into an infinite loop
        System.out.printf("%70s %n", " @@@@@     Zoos Australia     @@@@@ \n");
        System.out.printf("%57s %n", "Main Menu\n");
        System.out.printf("%72s %n", "Zoo has the following ticketing options: \n");
        System.out.printf("%59s %n", "1 = Child (4-5 yrs)");
        System.out.printf("%59s %n", "2 = Adult (18+ yrs)");
        System.out.printf("%60s %n", "3 = Senior (60+ yrs)\n");

        String choose1 = "";
        String choose2 = "";
        String choose3 = "";
        String selected = "";
        int option = 0;
        System.out.print("Please select an option: ");
        option = input.nextInt();
        if (option == 1) {
            choose1 = "Child";
            selected = choose1;
        } else if (option == 2) {
            choose2 = "Adult";
            selected = choose2;
        } else if (option == 3) {
            choose3 = "Senior";
            selected = choose3;
        }
        System.out.println(); // "\n" is a redundant argument

        int price = 0;
        int tickets = 0;
        System.out.print("Enter the number of tickets: ");
        tickets = input.nextInt();
        if (selected.equals(choose1)) { // you should never compare strings with == operator! Always use .equals() instead
            price = 10;
        } else if (selected.equals(choose2)) {
            price = 20;
        } else if (selected.equals(choose3)) {
            price = 15;
        }
        System.out.println();
        System.out.print("You are purchasing " + tickets + " " + selected + " tickets at " + "$" + price + " each!");
        System.out.println();

        int confirm = 0;
        System.out.print("Press 1 to confirm purchase: ");
        confirm = input.nextInt();
        if (confirm != 1) {
            System.out.print("Incorrect Key. Please return to Main Menu");
            System.out.println("\n");
        } else {
            //break; you cannot use 'break' in the if statement! You have to figure out another way, how to handle an invalid input
        }
        System.out.println();

        int total = tickets;
        price = total * price;
        System.out.print("Total amount for " + selected + " tickets: " + "$" + price);
        System.out.println();

        String pick = "";
        System.out.print("Do you wish to continue: ");
        pick = input.next(); // you have to assign the input to a variable
        System.out.println();

        if (pick.equals("no")) { // You have to ALWAYS use .equals() when comparing Strings or any other reference types! == works correctly only with primitive types
            System.out.print("Total amount payable is: " + "$" + price);
            System.out.println();
            System.out.print("Have a nice day!");
            System.out.println();
            break; // you need to break from the loop in the end
        }
    }
}
  • Wow- thank you so much. it worked, obviously. Haha. Such a small change too. Is it like that for everyone? I am just starting to learn Java, so I am still a bit rough around the edges.. I just couldnt wrap my head around that. Maybe my knowledge of loops or something is lacking. Thank you for your advice as well- is there more information or advice you are able to provide to help my learning? I feel i am struggling to learn this, unless that is normal. Haha. I didnt know that what you suggested was needed or a requirement. And it seemed like basic knowledge. Anyway, thanks again! – wallace Apr 24 '20 at 01:34
  • @wallace don't worry about it, it is totally normal not to know something. And often it's indeed just small and obvious mistakes, which can make you go crazy though :) The most important thing is to stick to it and know how to google stuff. Also, in the beginning, it's crucial to understand the basics well, to have a good structure without gaps. For learning Java, I can recommend hyperskill.org, it helped me a lot. I am glad, I could help. May I also ask you to mark my answer as accepted - I'm trying to earn reputation points here ;) – Yuliya Sheludyakova Apr 24 '20 at 13:44