简体   繁体   中英

What return statement should I utilize in order to use a Menu that throws an exception for non integer values?

I am working on a to do list and am currently stuck making a menu. The menu receives input from the user of the numbers 1-6 and carries out a specific task associated with that number(int). That's the perfect world scenario, so I need the menu to be able to take non integer values and not be bricked as well as display an error message to the user. I think I have created an efficient way of asking the user for integers without bricking the program but I cannot determine what my return statement should be in order to utilize the method in the main. I'll use it in a switch statement like this:

         while (true) {
        switch (getMenuOption()) {
            case 1:
            etc

This is the current method that I have for the getMenuOption. What return statement should I use, or is there a more efficient way to carry this out?

package project1_martinez_adriel;

import java.util.Scanner;

public class getMenuOption {
    public static int getMenuOption() {    
 Scanner input = new Scanner(System.in);
        System.out.println(" 1. Create a new item \n 2. Mark an item as in progress \n 3. Mark an item as completed \n 4. List all to do items \n 5. Remove completed items \n 6. Exit \n What would you like to do? \n ");
        String value = input.nextLine();
        int num;
        try {
            num = Integer.parseInt(value);
            if (!(num == 1 || num == 2 || num == 3 || num == 4 || num == 5 || num == 6)) {
                System.out.println("ERROR! Invalid choice! \nPlease enter a valid choice BETWEEN 1 & 6: ");
            }else if (num == 6){
            System.exit(0);
        }


        } catch (NumberFormatException e) {
            System.out.println("ERROR! Please enter a valid INTEGER between 1 & 6.");
        }

        return //What do I put here!?

    }

how about cleaning it up to be

if (num < 1 || num > 6) {
   System.out.println("ERROR! Invalid choice!...");
}

then later

return num;

The code in your switch statement should process the options between 1 && 6 including 6 being System.exit (0);

I would even have the error messages in the switch default block

edit

num should also be initialized with a value, something like

int num = -1;

So after some clean up, frustration, & long hours I have come up with this, including the switch statements:

Scanner input = new Scanner(System.in);
        boolean validInput = false;
        do {
            System.out.print("Enter an integer: ");
            int num;
            try {
                num = input.nextInt();
                switch (num) {
                  case 1:
                  case 2:
                  case 3:
                  case 4:
                  case 5:
                  case 6: // cascading case statement example
                    validInput = true;
                  break;
                  default:
                    System.out.println("ERROR! Please enter a valid choice BETWEEN 1 & 6 (inclusive): ");
                    num = input.nextInt();
                  break;
                }
            } catch (Exception e) {
                /* input.next() to move the Scanner forward. */
                System.out.println(input.next() + " was not valid input.");
                System.out.println("ERROR! Please enter a valid INTEGER between 1 & 6.");
            }
        } while (!validInput);
        input.close();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM