[Java] If-else involving strings

  • Context: Java 
  • Thread starter Thread starter Deathfish
  • Start date Start date
  • Tags Tags
    Java Strings
Click For Summary

Discussion Overview

The discussion revolves around the proper way to compare strings in Java, particularly in the context of an if-else statement that checks for specific string values representing colors. Participants explore different methods for string comparison and the implications of using incorrect comparison techniques.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant presents a code snippet that always returns "Invalid input," questioning the reason behind this behavior.
  • Another participant asserts that the issue arises from using the '==' operator for string comparison, suggesting that this is not the correct method in Java.
  • A participant mentions the memory storage of string literals in Java, comparing it to C and C++, indicating that string literals may evaluate to memory addresses.
  • One suggestion is made to use the method color.toUpperCase().equals("R") for comparison, which is presented as a better approach.
  • Another participant notes that if the variable color were of type char, the original comparison using '==' would be valid, emphasizing the difference between string and character literals.
  • A later reply provides a corrected version of the original code using color.equals() for string comparison, indicating that this will work correctly.

Areas of Agreement / Disagreement

Participants generally agree that the original method of string comparison using '==' is incorrect, but there are multiple proposed solutions and methods for achieving the desired functionality, indicating that the discussion remains somewhat unresolved regarding the best approach.

Contextual Notes

There is a lack of consensus on the best method for string comparison in Java, with various approaches suggested, including using equals(), toUpperCase(), and the implications of character versus string types.

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.");
}
 

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 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K