1

I tried to make a simple code in java for my project. I am learning java. What I want my code to do is that I want it to take an input of any day. If the inputted word is not a day then print it is wrong day. When i try it it doesn't work. Here is the code.

public class JAVA1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter day");
        String day = scanner.nextLine().toLowerCase();
        String day1 = day;
        System.out.println(day);
        switch(day) {
            case "friday": System.out.println("It is Friday");
            break;
            case "wednesday": System.out.println("It is Wednesday");
            break;
            case "thursday": System.out.println("It is Thursday");
            break;
            case "tuesday": System.out.println("It is Tuesday");
            break;
            case "monday": System.out.println("It is Monday");
            break;
            case "saturday": System.out.println("It is Saturday");
            break;
            case "sunday": System.out.println("It is Sunday");
            break;
        }
        System.out.println();
        if (day == "friday") {
            System.out.println("Wrong day lol");
        }
        else {
            System.out.println("Not wrong day lol");
        }
        
    }
}

The problem is in the if statement because if i type friday then it shows 'Not wrong day lol' And when i type friday it shows 'Wrong day lol', which is the exact opposite of what i have coded.

Be.eskay
  • 11
  • 2

1 Answers1

0

Replace day == "friday" to "friday".equals(day)

see String.equals versus ==

Kai-Sheng Yang
  • 1,371
  • 2
  • 12
  • 19