The logic is always failing

48 views Asked by At

It's a part to for which am entering the date 27 june ( to have the logic correct) , but still it prints the date is not correct(logic fails).

I don't understand why is it still failing.

**Code:**

Scanner date = new Scanner(System.in);
Scanner month = new Scanner(System.in);
System.out.println("Enter date");
int dat = date.nextInt();
String mon= "june";
//String month="feb";
System.out.println("now enter month");
String mont= month.nextLine();

if (dat== 27 && mont==mon) {
    System.out.println("yes thats the correct date");
}
else {
    System.out.println("no thats not the correct date");
}
2

There are 2 answers

0
Robby Cornelissen On

You need to compare objects (including strings) using equals() instead of ==:

if (dat== 27 && mont.equals(mon)){
  // ...
}
0
twentylemon On

The problem comes in your string comparison

String mont= month.nextLine();
if (dat== 27 && mont==mon){

mont==mon checks if mont and mon are literally the same object. Use mont.equals(mon)