-3

The line starting the if statement is causing me issues. When I run it it will ask me my gender, I say boy and it says 'you cannot enter'. It doesnt even ask for the age! import java.util.Scanner;

class Apples { 

//////Main Method
    public static void main (String[] args) { 

    if (Apples.getGender() == "boy" && Apples.getAge() > 17) { 
        System.out.printf("You can enter");
    }
    else { 
        System.out.printf("You cannot enter");
    }
}

/////Two Methods        
        public static String getGender() { 
        Scanner newGender = new Scanner(System.in);
        System.out.println("What gender are you?"); 
            String genderMF = newGender.nextLine();
            return genderMF;

        }

    public static int getAge() { 
        Scanner newAge = new Scanner(System.in);
        System.out.println("What age are you?");    
        int ageMF = newAge.nextInt();
        return ageMF;

    }
}   
RoseHaggar
  • 39
  • 3
  • In Java `String`s are compared using `equals(..)` not `==` eg: `Apples.getGender().equals("boy")` – Titus Jun 05 '15 at 00:38

1 Answers1

2

You have to compare strings with .equals function in Java, not with ==.

s1.equals(s2);
Francisco Romero
  • 12,787
  • 22
  • 92
  • 167