user3537993
user3537993

Reputation: 29

input validation and refuse improper input

I am attempting to validate user input. I have tried some if statements and attempted Boolean. I cant get anything to give the output or rather the validation I am wanting

Users are asked to choose between "red" or "blue" I want them to be required to type either "red" or "blue". I know this could be solved easier through buttons but I am trying to use string input. the code below give an example of where i am.

custColorMsg = "Which color do you want your shirt to be red or blue?";
customerColor = getstringInput(custColorMsg);
String color = null 
if( customerColor.equalsIgnoreCase("yes")) {
color = true}

if( customerColor.equalsIgnoreCase("no")) {
color = true}

else if(!customerColor.equalsIgnoreCase("yes" || "no") {
color = false}  

I know this last portion is wrong I am just unsure how to go about fixing it. Customers will have three chances to input a valid response then the program will terminate, this portion i can handle. If i can just figure out how to accept "yes" or "no" and refuse anything else.

Upvotes: 0

Views: 509

Answers (2)

Derek W
Derek W

Reputation: 10026

There's a few things wrong with your approach.

  1. The semantics of your variable names are a bit off. Which makes the code difficult to read. For example, the variable color which you have defined here as a String, but consistently use as a Boolean is a bit confusing. I'm guessing you mean to define it as a Boolean type and intend to use it as the breaking condition from your loop - it would be more meaningful to name the it as isValidColor or something along those lines.

  2. The following line doesn't do what you think it does:

    customerColor.equalsIgnoreCase("yes" || "no")

    The method equalsIgnoreCase() takes in a String and not a Boolean like this line of your code will have for an argument. Since the || will resolve to a Boolean value of true or false. Furthermore, those are bad operand types for that operator and the code won't compile.

  3. For your control structure you can use a while loop which will exit when you have reached the max amount of tries or entered a valid response.

Here's a working Console version of what you are trying to accomplish:

String custColorMsg = "Which color do you want your shirt to be red or blue?";
String customerColor;
Boolean validInput = false;
Scanner in = new Scanner(System.in);
int tries = 0;
while (tries < 3 && !validInput)
{           
    System.out.println(custColorMsg);
    customerColor = in.nextLine();

    if( customerColor.equalsIgnoreCase("red")) {
        validInput = true;
    }
    else if( customerColor.equalsIgnoreCase("blue")) {
        validInput = true;
    }

    tries++;
}

Upvotes: 1

Jake Chasan
Jake Chasan

Reputation: 6550

In terms of the design of this program, I would recommend adding a for loop, that goes from 0 to 2 (this will iterate 3 times). Within the loop, the program can determine what the user's input is. I would also recommend looking at my syntax for the for loop below, I use ifs, else ifs and elses to evaluate the data set more efficiently.

The implementation of the program could be:

for(int i = 0; i < 3; i++)
{
      customerColor = getstringInput(custColorMsg);
      String color = null 
      if( customerColor.equalsIgnoreCase("yes")) {
      color = true;
      break;
    }

      else if( customerColor.equalsIgnoreCase("no")) {
      color = true;
      break;
    }
      else{
      color = false;
    }
      custColorMsg = "Invalid Input, Please Input Again";
}

This will give the user 3 times to input the data, and if they input it correctly, it will stop asking, however, if they do not, it will ask again until they run out of attempts.

Upvotes: 1

Related Questions