Welcome to the CSC Q&A, where you can get help (and share your knowledge) about computer science!

Is there a way to use "not equal to" for strings?

+15 votes

I know that you can test to see if two values aren't equal with !=, but that only works with primitive data types and not Strings.

I also know that you can test to see if a String object "equals" something by using string.equals("text").

But is there a command to test if a string doesn't equal something?

asked Jan 15, 2016 in (Winter 15-16) by Gabriel Lance (100 points)
edited Jan 15, 2016 by Gabriel Lance

1 Answer

+3 votes
I think (not completely sure) that you would put the exclamation mark before the string name. For example:

if (!TimeOfDay.equals("Night")) {

That is my guess at it.
answered Jan 16, 2016 by Ryan McCarthy (100 points)
Note: proper capitalization for the variable would be timeOfDay
...