[Java] If-else involving strings

In summary, the conversation discusses an if-else code involving strings and the issue of always returning "Invalid input." regardless of the input. The proper way to compare strings in Java is using the .equals() method, and using character literals should be delimited by single quotes.
  • #1
Deathfish
86
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
  • #3
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.
 
  • #4
if (color == "R" || color == "r")

is better written as:

if(color.toUpperCase.Equals("R"))
 
  • #5
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.
 
  • #6
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.");
}
 

1. How do I compare two strings in Java using an if-else statement?

To compare two strings in Java, you can use the equals() method. This method compares the actual contents of the strings and returns a boolean value indicating whether they are equal or not. You can use this method in an if-else statement to perform different actions based on the comparison result.

2. What is the difference between using == and equals() to compare strings in Java?

The == operator compares the references of two objects, while the equals() method compares the actual contents of the strings. This means that using == may not always give the expected result when comparing strings, as it checks if the two strings are stored at the same memory location rather than their values.

3. Can I use if-else statements with multiple strings in Java?

Yes, you can use if-else statements with multiple strings in Java. You can use the equals() method to compare each string individually and perform different actions based on the results.

4. How do I handle case sensitivity when comparing strings in Java?

By default, the equals() method in Java is case-sensitive. This means that two strings with the same characters but different cases will not be considered equal. To handle case sensitivity, you can use the equalsIgnoreCase() method, which compares the strings while ignoring their cases.

5. What happens if I use an if-else statement without an else block in Java?

If you use an if-else statement without an else block, then the code inside the if block will only be executed if the condition is true. If the condition is false, then the code inside the else block will not be executed. In this case, if the condition is false, the program will simply continue to the next line of code after the if-else statement.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
3
Views
773
  • Programming and Computer Science
Replies
19
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
6K
  • Programming and Computer Science
Replies
14
Views
3K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
2
Replies
55
Views
4K
  • Programming and Computer Science
Replies
2
Views
5K
Back
Top