First of all, it looks like you are dealing with the wrong variable sc
. I think you meant to compare mood
.
When dealing with strings, always use .equals()
, not ==
. ==
compares the references, which is often unreliable, while .equals()
compares the actual values.
It's also good practice to either convert your string to all uppercase or all lower case. I'll use lower case in this example with .toLowerCase()
. .equalsIgnoreCase()
is also another quick way around any case problems.
I'd also recommend an if-else-statement
, not a second if-statement
. Your code would look like this:
mood=mood.toLowerCase()
if (mood.equals("happy")) {
System.out.println("test");
}
else if (mood.equals("sad")) {
System.out.println("I am sad");
}
These are all pretty basic java concepts, so I'd recommend reading more thoroughly about some of them. You can check out some of the documentation and/or other questions here:
- if-else statement
- Strings
- Java String.equals versus ==