0

I am completely new to Java and I am trying to get the user input to check my array for valid input and continue code and if it's not valid to repeat until I get valid input.

public class Cheesecake {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        float biteSize = 3;                                                     
        float small = 9;
        float large = 12;
        String chosenSize;
        double pricePerInch = 0;
        double total = 0;
        String[] chooseSizes = {"bite size", "small", "large"};
        String[] chooseFlavors = {"plain", "strawberry", "raspberry", "caramel", "chocolate"} ;



        Scanner scnr = new Scanner(System.in);

        System.out.println("Flavors to choose from: plain, strawberry, raspberry, caramel, chocolate.");        //giving user flavor list

        /*System.out.println("Please choose flavor:");                                          

        flavors = scnr.nextLine();*/

        String flavors;
        while (true) {
            System.out.println("Please choose flavor:");                                            
            flavors = scnr.nextLine();
            if (flavors.equals(chooseFlavors))
                break;
            else
                System.out.println("Please choose from flavors above.");
3
  • 1
    Hi David, I’m not sure you asked a question. Is there something wrong with your code? Also, please also always use { and } even on one line if statements. You will save yourself a lot of unnecessary hassle in the future. Commented Jul 9, 2018 at 21:00
  • Yes sorry the code was just looping over and over no matter if I enter something that matched my array or not Commented Jul 9, 2018 at 21:12
  • You have an answer below but basically you were trying to compare the user input with the whole array in one go and therefore would never be true. You need to loop over the array and individually check each one matches/doesn’t match the entry in the array. Commented Jul 9, 2018 at 21:14

1 Answer 1

1

You have to check each index of the array with your flavors. (Should probably rename to userFlavor).

String[] chooseFlavors = {"plain", "strawberry", "raspberry", "caramel", "chocolate"};
... 

boolean flavorFound = false;
while (!flavorFound) { // Loop until flavorFound is true
  System.out.println("Please choose flavor:");                                            
  userFlavor = scnr.nextLine();

  for(int i = 0; i < chooseFlavors.length; i++) { // Loop from 0 to choosFlavors.length
      if(userFlavor.equals(chooseFlavors[i]) { // Compare user input to flavor at index i
          System.out.println(userFlavor + " found at index " + i);
          flavorFound = true; // This is the flag to break out of while loop
          break; // Only breaks out of for loop
      } else {
          System.out.println("Please choose from flavors above.");
      }
  }

}
Sign up to request clarification or add additional context in comments.

2 Comments

thank you for the help I believe I asked question wrong tho I am getting it to see that its an array but it still continues to loop to the same question, I need it to continue on to the rest of my code but the rest says its unreachable code. Also, one more question after your suggestion of changing flavors to userFlavor the flavors before found at index should be userFlavor correct.
@David Correct, I'll change the code. The problem with it saying unreachable code is because of the while(true) loop. The break in my example will only break out of the for loop. To handle the while(true) we can set a flag to only loop until the input is correct. I'll update

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.