0

I need to do some data validation to ensure that the user enters one of the 4 these 4 choices (small,medium, large, super size) I never did data validation with Strings, must be there another way for this to work?

  public static String orderSize()
{
System.out.println("What size - small, medium, large, or super size?"); 
    String sizeChoice=input.nextLine();
    while (sizeChoice != "small" + "medium" + "large" + "super"); 
    {
        System.out.println("Must be one of the sizes listed above here.");
        System.out.println("What size - small, medium, large, or super size?");
        sizeChoice=input.nextLine();

    }
    return sizeChoice;
2
  • In java, "+" with Strings denotes concatenation - so your while statement is equivalent to while (sizeChoice != "smallmediumlargesuper"). Also, get rid of the semicolon at the end of that while line - it terminates the statement - which IS the while, which means the block in the braces is not in the while loop at all ! Commented Dec 12, 2018 at 0:55
  • If there is an answer below that most suitably answers your question, then please consider to accept that answer Commented Dec 12, 2018 at 1:56

5 Answers 5

3

You're quite off to be honest, what I'd suggest is to first create a set holding the possible choices.

Set<String> choices = new HashSet<>(Arrays.asList("small","medium","large", "super"));

Then simply change your while loop condition to:

 while (!choices.contains(sizeChoice)){...} // while the chosen size is not in the set of choices then keep looping...
Sign up to request clarification or add additional context in comments.

1 Comment

Good Idea. I didn't think it.
1

I would go for a simple easy to read switch statement (unlikely that this is an issue, but note that you require Java 7+ to use switch with Strings)

boolean validated = false;
while (!validated); 
{
    switch (sizeChoice) {
        case "small":
        case "medium":
        case "large":
        case "super":
            validated = true;
            break;
        default: 
            System.out.println("Must be one of the sizes listed above here.");
            System.out.println("What size - small, medium, large, or super size?");
            sizeChoice=input.nextLine();
    }
}

Comments

0

I would do it this way:

public static int orderSize()
{
    System.out.println("What size?  \n 1. small \n 2. medium \n 3. large 4. super \n 5. cancel"); 
    int sizeChoice=input.nextInt();
    while (sizeChoice < 1 || sizeChoice > 4 ) 
    {
        System.out.println("Must be one of the sizes listed above here.");
        System.out.println("What size?  \n 1. small \n 2. medium \n 3. large 4. super \n 5. cancel");
        sizeChoice=input.nextInt();
        if(sizeChoice == 5) break;

    }
    return sizeChoice;
}

Comments

0

It looks like you want to use the Conditional-AND operator.

!"small".equals(sizeChoice) && !"medium".equals(sizeChoice) && !"large".equals(sizeChoice ) && !"super".equals(sizeChoice)

As noted by other users there is always a clever way to do the same thing. Your code also suites a do/while loop better than a while loop if you want to avoid repeating yourself.

public static String orderSize() {
    String sizeChoice = null;
    do {
        if (sizeChoice != null) {
            System.out.println("Must be one of the sizes listed.");
        }
        System.out.println("What size - small, medium, large, or super size?");
        sizeChoice = input.nextLine();
    } while (...);
    return sizeChoice;
}

Comments

-1

If you do validation, I assume you have a controller. If it's only 4, I'd change the input type to an Enum, and validation will work out of the box.

The following is only to illustrate what I meant, it's dummy code, and will probably not be 100% correct.

@Controller  
public class ChoiceController {

   @GetMapping("/choose")
   public void choose(SizeEnum size) {
       // rest of the owl here
   }
}

Comments

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.