简体   繁体   中英

Java- how to continue asking for user input after try catch?

I am making a project with a simple main menu. I have to check validation for of the user input : 1)if he entered an option between 1 and 7 (did it) 2)if he entered an integer number.

for number 2 I wanted to use try and catch, but did not manage to success. after the user entered a non integer number , it prints the exception (in the catch block), and returns back to the main. I want to keep asking the user for an input untill he enters 1-7

public void SystemExecute() {

    System.out.println("Welcome to our project!");
    int userSelection = 0;
    Boolean incremental;

    try {
        while (userSelection != 6) {

            printMenu();
            userSelection = getUserSelectionFromMenu();

            switch (userSelection) {
                case 1: {
                    loadFile();
                    break;
                }
                case 2: {
                    printGraphInformation();
                    break;
                }
                case 3: {
                    printTargetInformation();
                    break;
                }
                case 4: {
                    printTargetConnectionStatus();
                    break;
                }
                case 5: {
                    incremental = askForIncremental();
                    try {
                        TaskExecuting.executeTask(graph, incremental, graphSummary);
                    } catch (OpeningFileCrash | FileNotFound e) {
                        System.out.println(e.getMessage());
                    }
                    firstRun = false;
                    break;
                }
                case 6: {
                    exitFromSystem();
                    break;
                }
                case 7: {
                    saveSystemStatus();
                    break;
                }
                default: {
                    System.out.println("Please enter an option between 1-7!\n");
                    break;
                }
            }
        }
    } catch (InputMismatchException e)
    {
        System.out.println("Please enter an integer number !");
    }

}

Make the try catch insid of while loop :

public void SystemExecute() {
                        System.out.println("Welcome to our project!");
        int userSelection = 0;
        Boolean incremental;

        while (userSelection != 6) {
            try {
                printMenu();
                userSelection = getUserSelectionFromMenu();
                switch (userSelection) {
                    case 1: {
                        loadFile();
                        break;
                    }
                    case 2: {
                        printGraphInformation();
                        break;
                    }
                    case 3: {
                        printTargetInformation();
                        break;
                    }
                    case 4: {
                        printTargetConnectionStatus();
                        break;
                    }
                    case 5: {
                        incremental = askForIncremental();
                        try {
                            TaskExecuting.executeTask(graph, incremental, graphSummary);
                        } catch (OpeningFileCrash | FileNotFound e) {
                            System.out.println(e.getMessage());
                        }
                        firstRun = false;
                        break;
                    }
                    case 6: {
                        exitFromSystem();
                        break;
                    }
                    case 7: {
                        saveSystemStatus();
                        break;
                    }
                    default: {
                        System.out.println("Please enter an option between 1-7!\n");
                        break;
                    }
                }
            }
            catch (InputMismatchException e)
            {
                System.out.println("Please enter an integer number !");
            }
    }
}

将 try catch 块放入 while 循环中。

public void SystemExecute() {
        System.out.println("Welcome to our project!");
        int userSelection = 0;
        Boolean incremental;

        do{
            try {
                printMenu();
                userSelection = getUserSelectionFromMenu();
                switch (userSelection) {
                    case 1: {
                        loadFile();
                        break;
                    }
                    case 2: {
                        printGraphInformation();
                        break;
                    }
                    case 3: {
                        printTargetInformation();
                        break;
                    }
                    case 4: {
                        printTargetConnectionStatus();
                        break;
                    }
                    case 5: {
                        incremental = askForIncremental();
                        try {
                            TaskExecuting.executeTask(graph, incremental, graphSummary);
                        } catch (OpeningFileCrash | FileNotFound e) {
                            System.out.println(e.getMessage());
                        }
                        firstRun = false;
                        break;
                    }
                    case 6: {
                        exitFromSystem();
                        break;
                    }
                    case 7: {
                        saveSystemStatus();
                        break;
                    }
                    default: {
                        System.out.println("Please enter an option between 1-7!\n");
                        break;
                    }
                }
            } while(userSelection != 6)
            catch (InputMismatchException e)
            {
                System.out.println("Please enter an integer number !");
            }
    }
    }

I would rather recommend doing a do-while rather than a while loop

Look at the problem differently. First get the user input AND validate it, then proceed to do whatever you need to with it. Put the whole thing in a do { ... } while (condition) loop to have it read in at least once

  1. Read in an integer from the keyboard or from whatever you're reading from
  2. Branch as appropriate based on your cases. Make each branch have its own method to keep things simple and readable. Put any try / catch logic directly in there.

For example:

boolean quit = false;
do {
    int choice = getUserSelectionFromMenu();
    switch (choice) {
        case 1:
            case1Logic();
            break;
        // other cases here
        case 6:
            quit = true;
            break;
       // other cases here
        default:
            // handle invalid user input here
            break;
    }
} while (!quit);

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