0

im writing a code that depending on your age and gender,it will display if you can enlist in the army,however for some reason after reading the first statement it will already jump to the result without evaluating the second condition. here is the code:

package exercicios1;
import java.util.Scanner;

public class Idade
{
private int Age;
private String Gender;

public int getAge() 
{
    return Age;
}

public void setIdade(int age) 
{
    Age = age;
}

public String getGender()
{
    return Gender;
}

public void setGender(String G)
{
    Gender = G;
}

public void ageVerification()
{
    Scanner input = new Scanner(System.in);
    System.out.print("Please enter your age: ");
    Age = input.nextInt();
    System.out.print("Please enter your gender(M/F) :");
    Gender = input.nextLine();

    if(18 <= Age & Age <= 27 & Gender.equals("m"))
    {
        System.out.printf("You are viable to be enlisted");
    }
    else
    {
        System.out.printf("You are not viable to be enlisted");
    }
}
Agent
  • 17
  • 2
  • 2
    See http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods – Alexis C. Sep 26 '15 at 22:22

1 Answers1

1

Add this line after Age = input.nextInt();

input.nextLine();
AsSiDe
  • 1,749
  • 2
  • 13
  • 23