简体   繁体   中英

try catch/user input for Java

What I'm trying to do is create a loop that prints out a schedule then another loop that will go week to week in that schedule. What my issue is, is that when I try to put in the prompt of "Start", the code doesn't continue after I hit enter the first time and when it does it just goes back to the first loop.

Here's my code. Tell me if I need to show more

public class WorkoutDriver {
    public static void main(String[] args) {
        boolean run = true;
        boolean startRun = true;
        System.out.println("************************************************" +
                "\n*** Welcome to your customized workout plan! ***" +
                "\n************************************************");


        Scanner userInput = new Scanner(System.in);
        int userNum;
        String userStart;
        while(run){
            try{
                System.out.println("How many weeks would you like to schedule?");
                userNum = userInput.nextInt();

                WorkoutPlan plan = new WorkoutPlan(userNum);
                if(userNum > 0){
                    userInput.nextLine();
                    plan.fillList();
                    System.out.println("Great lets look at your " + userNum + " week schedule!\n");
                    System.out.println(plan);


                    //loops to have user go through each week
                    int weekCount = 1;
                    System.out.println("Time to start working out!");
                    while(weekCount <= userNum) {
                        System.out.println("Type \"Start\" to complete one week of workouts:");
                        userStart = userInput.nextLine();
                        if (userStart.equalsIgnoreCase("start")) {
                            userInput.nextLine();
                            plan.workoutNextWeek(userNum - 1);
                            plan.printProgress();
                        } else {
                            System.out.println("Don't worry you got this!");
                        }
                        weekCount++;

                    }




                }else{
                    System.out.println("Please enter a number higher than 0");
                    System.out.println();
                }


            }catch (ArrayIndexOutOfBoundsException e){
            System.out.println("Please try again, enter a valid integer");
            userInput.nextLine();
            }catch (Exception e){
            System.out.println("Please try again, enter a valid integer");
            userInput.nextLine();
            }
        }
    }
}

First you are using a userInput.nextLine(); without putting it's value into a variable, and then you do the same thing inside your while-loop. Maybe removing them should do the job. Also on your catch statement what you should be using is userInput.next(); not userInput.nextLine();

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