0

Basically, what I want my code to do is if the user inputs anything besides "sum, quit, max, min" I want the program to loop back and ask the user the options again. I tried using a while-loop but I can't add multiple String objects within it. I'm stuck on what to do. (Also, I used if-statements instead of while so it won't continually repeat and be stuck.)

EDIT: thank you for the help! I used a while(true), break statements, and a continue at the end and it seems to work!

public static void main (String [] args){
        System.out.println("Enter the option sum, max, min:");
        Scanner input = new Scanner (System.in);
        user = input.nextLine();
        double [] y = theArray();


        if (user.equals("sum")){
            //do something

        }
        else if (user.equals("max")){
            //do something 
        }
        else if (user.equals("min")){
            //do something
        }
        else if (user.equals("Quit")){
            //do something
        }


    }
} 
5
  • What do you mean by "I can't add multiple String objects within it"? Commented Oct 26, 2017 at 21:24
  • within a while loop you cant say while(user.equals("quit", "sum"); Commented Oct 26, 2017 at 21:27
  • Use the while infinite loop and break it when the condition is met. Commented Oct 26, 2017 at 21:27
  • @csstudent2x oh, I get it. I didn't understand that's what he meant. Commented Oct 26, 2017 at 21:28
  • Just a comment on your note with the while statement; although you can't say while(user.equals("quit", "sum")){ you can say while(user.equals("quit") || user.equals("sum")){ , and add more ||'s (Or's) as needed for anything else. If that logic makes more sense to you than the break or the recursion answers below, give it a try. Commented Oct 26, 2017 at 21:41

2 Answers 2

1

As I have said in the comment above, use the while infinite loop and break it when the condition is met. If the condition is not met, the loop goes for the next iteration. Keep aware what should be looped and why.

System.out.println("Enter the option sum, max, min:");
Scanner scanner = new Scanner(System.in);
String option;

while (scanner.hasNextLine()) {
    System.out.println("Enter the option sum, max, min:");
    option = scanner.nextLine();
    if ("sum".equals(option)) {
        System.out.println("Sum entered, loop ended");
        // do something
        break;
    } else if ("max".equals(option)) { // the same goes for min, max, quit }
}
Sign up to request clarification or add additional context in comments.

Comments

0

I would suggest using recursion:

public static void main (String [] args){

        Scanner input = new Scanner (System.in);

        promptUser(input);

        System.out.println("done");

    }
    public static void promptUser(Scanner input) {
        System.out.println("Enter the option sum, max, min:");
        String user = input.nextLine();
        double [] y = theArray();


        if (user.equals("sum")){
            //do something
        }
        else if (user.equals("max")){
            //do something
        }
        else if (user.equals("min")){
            //do something
        }
        else if (user.equals("Quit")){
            //do something
        }else {
            //if wrong input is entered, try again
            promptUser(input);
        }
    }

1 Comment

Any idea if Java would optimize this into a tail recursive form?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.