0

I am new in core java and wrote a nested if statement code. I'm facing the problem when nested condition is satisfied and print statement "you will get 40% discount of fair" is not getting executed.

import java.util.Scanner;

public class Ifstatement {
    static int age ;
    static String Gender;

    public static void main(String[] args) {
        Scanner obj = new Scanner (System.in);
        System.out.print("Enter the age: ");
        age = obj.nextInt();
        System.out.print("Enter the Gender: ");
        Gender= obj.next();
        System.out.println("Gender is "+ Gender);
        System.out.println("Age is "+ age);
        
        if (age<=14) 
        {
            System.out.println(" you will get 30% discount of fair ");
            
            if(Gender== "F") 
            {
                System.out.println("you will get 40% discount of fair");
            }
            
        }
        
        if (age>=14) 
        {
            System.out.print("you will get 20% discount of fair");
        }
        
    }
}
Traxidus Wolf
  • 808
  • 1
  • 9
  • 18
  • 2
    Change nested if condition to `if("F".equals(Gender))` – Alex Apr 24 '21 at 05:04
  • 2
    @Alex is correct. When you compare a Class (String), you must use ".equals()", but if you compare primitives (int) you must use "==". You do comparison in 2 different ways. – davidalayachew Apr 24 '21 at 06:08

0 Answers0