#try and catch error

1 messages · Page 1 of 1 (latest)

sharp elm
#

For some reason the error for try and catch pops up when i first run the program without inputting anything for the try and catch to detect. checked the error with e.LocalizedMessage and got this error?

    public static void orderFlow(restaurant restaurant){
        System.out.println("\nWelcome to " + restaurant.getName());
        restaurant.printMenu();

        order order = new order();

        while (true){
            System.out.print("Choose item number, 0 to back, or 'done': ");
            String choice = scanner.nextLine();

            if (choice.equals("0")){
                return;
            }
            else if (choice.equalsIgnoreCase("done")){
                break;
            }
            int test;
            try {
                test = Integer.parseInt(choice) - 1;
            }
            catch (Exception e) {
                System.out.println("Invalid!" + e.getLocalizedMessage());
                continue;
            }

            if (test < 0 || test >= restaurant.getMenu().size()) {
                System.out.println("Invalid!");
                continue;
            }
brazen topazBOT
#

<@&987246964494204979> please have a look, thanks.

sacred marlin
#

i suspect ur using scanner.nextInt() somewhere or similar

sharp elm
#

uh

sacred marlin
#

instead of sticking to scanner.nextLine() everywhere

sharp elm
#

zip file?

#

there's uh

sacred marlin
#

i just need the file where the scanner is used

sharp elm
#

oh alright

sacred marlin
#

btw, u shouldnt catch all exceptions, only the one u expect

#

in this case a NumberFormatException

#

(u might also e.printStackTrace() so u get the full info in the console)

sharp elm
#

oops

brazen topazBOT
# sharp elm

I uploaded your attachments as Gist. This makes them more accessible, for example to mobile users.

sharp elm
sacred marlin
#

always only use nextLine(), nothing else. in particular dont mix it

#

details here:

brazen topazBOT
#

Mixing any nextXXX method with nextLine from the Scanner class for user input, will not ask you for input again but instead result in an empty line read by nextLine.

To prevent this, when reading user input, always only use nextLine. If you need an int, do

int value = Integer.parseInt(scanner.nextLine());

instead of using nextInt.

Assume the following:

Scanner scanner = new Scanner(System.in);

System.out.println("Enter your age:");
int age = scanner.nextInt();
System.out.println("Enter your name:");
String name = scanner.nextLine();

System.out.println("Hello " + name + ", you are " + age + " years old");

When executing this code, you will be asked to enter an age, suppose you enter 20.
However, the code will not ask you to actually input a name and the output will be:

Hello , you are 20 years old.

The reason why is that when you hit the enter button, your actual input is

20\n

and not just 20. A call to nextInt will now consume the 20 and leave the newline symbol \n in the internal input buffer of System.in. The call to nextLine will now not lead to a new input, since there is still unread input left in System.in. So it will read the \n, leading to an empty input.

So every user input is not only a number, but a full line. As such, it makes much more sense to also use nextLine(), even if reading just an age. The corrected code which works as intended is:

Scanner scanner = new Scanner(System.in);

System.out.println("Enter your age:");
// Now nextLine, not nextInt anymore
int age = Integer.parseInt(scanner.nextLine());
System.out.println("Enter your name:");
String name = scanner.nextLine();

System.out.println("Hello " + name + ", you are " + age + " years old");

The nextXXX methods, such as nextInt can be useful when reading multi-input from a single line. For example when you enter 20 John in a single line.

sharp elm
#

ohhhhhh

#

i see, alright thank you

sacred marlin
#

worth noting that in modern java this isnt really an issue anymore

#

since ur meant to use IO.println and IO.readLine instead

#

not scanner

#

(but that requires java 25+)

sharp elm
#

ah i see, alright then thank you

#

maybe next semester im gonna update to java 25+ since i dont want to break this coding yet, im working with my friend and im afraid me updating to java 25+ will break their codes too