monir suliman
monir suliman

Reputation: 21

data validation with Strings

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;

Upvotes: 0

Views: 95

Answers (5)

Jake Holzinger
Jake Holzinger

Reputation: 6063

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;
}

Upvotes: 0

Scary Wombat
Scary Wombat

Reputation: 44824

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();
    }
}

Upvotes: 1

Matter
Matter

Reputation: 36

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;
}

Upvotes: 0

Erik Pragt
Erik Pragt

Reputation: 14617

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
   }
}

Upvotes: -1

Ousmane D.
Ousmane D.

Reputation: 56423

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...

Upvotes: 3

Related Questions