-9

why isnt this working

package com.company;
import java.util.Scanner;

public class Main {

  public static void main(String[] args) {
  // write your code here
    Scanner scan = new Scanner(System.in);
    System.out.println("enter year: ");
    int year = scan.nextInt();
    int age = 0;
    System.out.println("is your bday complete this year: ans 'Y/N'");
    String bday= scan.nextLine();
    scan.close();
    if (bday=="Y"||bday=="y"){
        age = 2021-year;
    }else if(bday=="N"||bday=="n"){
        age = 2020-year;
    }else{
        System.out.println("wrong input");
    }
    System.out.println(age);
  }
}
Scott Hunter
  • 48,888
  • 12
  • 60
  • 101

1 Answers1

2

You need to use equals() method to compare strings. In this case it is better to use equalsIgnoreCase(). Update the if and else if as below:

if (bday.equalsIgnoreCase("y")){
    age = 2021-year;
}else if(bday.equalsIgnoreCase("n")){
    age = 2020-year;
}
Gautham M
  • 4,816
  • 3
  • 15
  • 37