0

Hello!

public static void main(String[] args) 
	{
		System.out.println("Type the operation system (Windows or Linux):");
		Scanner oku= new Scanner(System.in);
		String os=oku.next();
		if(os=="Windows")
		{
			CreateForWindows();
		}
		if(os=="Linux")
		{
			CreateForLinux();
		}
		else
		{
			System.out.println("Wrong selection. Program closed.");
			return;
		}
		return;

It always show me Wrong selection. Program closed. I can not understand why it does not go inside on any if block. I printf also the String os to see if it is equals on windows or Linux, which shows me exactly what i type. How that possible so simple thing...

3
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by sirin_adam
0

Yes: It is important to always compare strings using the '.equals' method -- not the '==' operator.

(And in Java, the convention is to start method names with a lower case letter. ;-)

There is something to be said for doing the string comparisons like this:

public static void main(String[] args) {
		System.out.println("Type the operation system (Windows or Linux):");
		Scanner oku = new Scanner(System.in);
		String os = oku.next();
		if ("Windows".equalsIgnoreCase(os)) {
			createForWindows();
		} else if ("Linux".equalsIgnoreCase(os)) {
			createForLinux();
		} else {
			System.out.println("Wrong selection. Program closed.");
			System.exit(1);
		}
		return;
	}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.