0

Sorry if the title doesn't make sense. Basically, I'm a noob, and for learning purposes I'm trying to create a basic program which asks first for gender then for age to determine access. The program is sexist for testing purposes. Anyway, heres what I have:

    System.out.println("What is your Gender?");
    String gender = input.nextLine();
        if (gender.equals ("male")){
            System.out.println("What is your age?");
            int agem = input.nextInt();
            if (agem >= 21 && agem < 65){
                System.out.println("Access Granted");
            }else{
                System.out.println("Access Denied");
            }
        }
        if (gender.equals ("female")){
            System.out.println("What is your age?");
            int agef = input.nextInt();
            if (agef >=18 && agef < 60){
                System.out.println("Access Granted");
            }else{
                System.out.println("Access Denied");
            }
        }

Now what I am trying to do is create a response in case the user enters something other than "male" or "female". I've tried using else in the case that both if statements are false but it hasn't worked.

I also tried creating another if statement like this one:

if (!gender.equals ("male") || ("female"));

But that doesn't work either.

Thanks

5 Answers 5

2

as an immediate solution, you need to add following condition in the end

if (!(gender.equals ("male") || gender.equals("female"))); or

if (!gender.equals ("male") && !gender.equals("female"));

but to avoid unnecessary condition check the whole condition should look like this:

        if (gender.equals ("male")){
            //
        }
        else if (gender.equals ("female")){
            //
        } else {

        }

and since java 7, you can also use string bases switch statement:

switch(value){
    case "male":
        //todo
        break;
    case "female"
        //todo
        break;
    default:
        //todo
}
Sign up to request clarification or add additional context in comments.

2 Comments

Your first statement should be if (!gender.equals ("male") && !gender.equals("female")). Otherwise, it will always evaluate to true
if (!(gender.equalsIgnoresCase("male") && gender.equalsIgnoresCase("female"))) will be even better to guard against input like "Male" i feel.
1

You can also go with a switch:

switch(gender) {
  case "male": 

    break;

  case "female":

    break;

  default: //everything else
}

Edit: string switch works since java 7

Comments

1

this will work:

if (gender.equals ("male") || gender.equals("female"))
{ 
   //input is okay, do something
} 
else 
{ 
  //input is not "male" or "female"  
}

1 Comment

@wyles, Those are not equivalent. Gender cannot both equal "male" and "female," so that would always be false. Then you are negating it, always returning true. Thus the block would always be executed.
0

You may want to restructure your program to use if, else if, else.

For example:

System.out.println("What is your Gender?");
String gender = input.nextLine();
if (gender.equals ("male")){
    System.out.println("What is your age?");
    int agem = input.nextInt();
    if (agem >= 21 && agem < 65){
        System.out.println("Access Granted");
    }else{
        System.out.println("Access Denied");
    }
} else if (gender.equals ("female")){
    System.out.println("What is your age?");
    int agef = input.nextInt();
    if (agef >=18 && agef < 60){
        System.out.println("Access Granted");
    }else{
        System.out.println("Access Denied");
    }
} else {
    System.out.println("Unrecognized input.");
}

This is a little more succinct, as it's not possible for it to execute the female branch if the male branch has been executed, and it's not possible for it to execute the unrecognized branch if either the male or female branch has been executed.

Comments

0

The quickest solution to this would be to use:

if (gender.equals("Male")) {

} else if (gender.equals("Female")) {

} else {

}

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.