0

import java.util.Scanner;

 public class decisionMaking {

public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);
    System.out.println("Please enter a weekday");
    String Weekday = scanner.nextLine();
    
    if( Weekday =="Monday"){
    System.out.println("Uff" + " " + Weekday + " " + "working day");
        
    }
    else if( Weekday == "Tuesday") {
        System.out.println("Uff" + " " + Weekday + " " + "working day");
        
    }
    else if (Weekday == "Wednesday") {
        System.out.println("Uff" + " " + Weekday + " " + "working day");
        
    }
    else if( Weekday == "Thursday") {
        System.out.println("Uff" + " " + Weekday + " " + "working day");
        
    }
    else if( Weekday == "Friday") {
        System.out.println("Uff" + " " + Weekday + " " + "working day");
        
    }
    else if( Weekday == "Satuarday") {
        System.out.println("Yay" + " " + Weekday + " " + "Holiday");
        
    }
    else {
        System.out.println("Yay" + " " + "Sunday" + " " + "Holiday");
    }

    scanner.close();
}

}

even though i gave different weekday names it is printing only "Yay Sunday holiday"

I tried instead of scanner, declared variable value like String Weekday = " Monday"

Then it is working fine and Showing like " Uff its working day"

May i know the reason sir

May i know way it is not working

Goutham N
  • 1
  • 1
  • 1
    This has little to do with Scanner and all to do with, `if( Weekday =="Monday"){`, how you're comparing Strings. You're using `==` which compares if the two String *references* are one and the same, which is not what you want. You want to use the `.equals(...)` or `.equalsIgnoreCase(...)` methods to check if the two Strings have the same char content in the same order. This is a fairly common new-to-Java error. – Hovercraft Full Of Eels Jul 24 '23 at 13:30
  • 1
    for simple comparisons (one to one) like this, a switch block might be easier. But, either way, == and === are JavaScript approaches. In Java, == compares the references of the Strings, not their values. – Stultuske Jul 24 '23 at 13:46

0 Answers0