Java [Java] If-else involving strings

  • Thread starter Thread starter Deathfish
  • Start date Start date
  • Tags Tags
    Java Strings
Click For Summary
The discussion centers on a Java code snippet that incorrectly uses the '==' operator to compare strings, leading to consistent "Invalid input" messages regardless of user input. The correct method for string comparison in Java is to use the `.equals()` method, as string literals in Java are object references rather than direct values. An alternative approach mentioned is converting the input string to uppercase and comparing it to "R", which simplifies the comparison. Additionally, it is noted that if the variable were of type char, single quotes should be used for character literals. The corrected code examples demonstrate the proper use of `.equals()` for string comparison and highlight the importance of understanding how strings are handled in Java.
Deathfish
Messages
80
Reaction score
0
I have an if-else piece of code involving strings

String color = sc.nextLine();
if (color == "R" || color == "r"){
System.out.println("red");
}
else if (color == "G" || color == "g"){
System.out.println("green");
}
else if (color == "B" || color == "b"){
System.out.println("blue");
}
else{
System.out.println("Invalid input.");
}

Why is it that no matter what input I put in, it always returns invalid input?
 
Technology news on Phys.org
I'm not sure how string literals are stored in Java, but in C and C++, string literals evaluate to the address of their first byte in memory. IOW, a string literal such as "R" would evaluate to the location of the 'R' character.
 
if (color == "R" || color == "r")

is better written as:

if(color.toUpperCase.Equals("R"))
 
On the other hand, if color is of type char, you CAN do this:
Code:
if (color == 'R' || color == 'r')
{
   ...
}
else if ((color == 'G' || color == 'g')
{
   ...
}
...
Note that character literals are delimited by single quotes, not double quotes.
 
This will work.

if (color.equals("R")|| color.equals("r"))
{
System.out.println("red");
}
else if (color.equals("G") || color.equals("g"))
{
System.out.println("green");
}
else if (color.equals("B") || color.equals("b"))
{
System.out.println("blue");
}
else
{
System.out.println("Invalid input.");
}
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
7K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
17K