Question

if (comparison < 0 ) {
   // result is less
}
else if (comparison > 0) {
   // result is greater
}
else {
  System.out.println(something);
}

Hi there, quick question regarding the proper protocol with 'If' statements. The code above is sort of pseudo-code for what I'm writing.

Basically it compares two strings, if the strings are equal to each other, I want it to keep that information (system.out.println for now, but output written later).

This leaves me with an 'if' and an 'else if' that are of basically no use because I only want exact matches really.

What should be put between the {} of the 'if' and 'else if' (assuming there is a proper way to do this) or is there a better way to code this idea?

Thank you :)

Was it helpful?

Solution

Use String.equals, like this:

if (string1.equals(string2)) { System.out.println("Match!"); }

This gives you no superfluous if-statements.

Read more on the String class API.

OTHER TIPS

If you want to know if two strings are EXACTLY equals, you could do this:

if (string1.contentEquals(string2)) System.out.println(something);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top