문제

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 :)

도움이 되었습니까?

해결책

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.

다른 팁

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

if (string1.contentEquals(string2)) System.out.println(something);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top