-7

This is our simple Task in school but no one gets the the right output. The user just need to type in the word "Blue" but the problem is, the "if" statement wont occur.. (new in this site)

import java.util.*;
 public class emptyclass{
  public static void main (String[]args){
   
   Scanner in = new Scanner (System.in);
   
 System.out.println("Enter a Color:");
 String color = in.nextLine();
 
 
 if (color == "Black")
 {
  System.out.println("You chose color Black");
 }
 else 
   {
     System.out.println("Please Choose a color");
     }
  }
 }

2 Answers2

1

You have to use the equals method for string comparaison in java.

"Black".equals(color)

NiziL
  • 5,068
  • 23
  • 33
0

Just use color.equals("Black");

import java.util.*;
public class emptyclass{
public static void main (String[]args){

    Scanner in = new Scanner (System.in);

System.out.println("Enter a Color:");
String color = in.next();


if (color.equals("Black"))
{
System.out.println("You chose color Black");
}
else 
 {
   System.out.println("Please Choose a color");
 }
}
}

It is working.

subhakar patnala
  • 319
  • 1
  • 4
  • 15