-2
String ntext;
ntext = something;
String currentLine;
currentLine = something;

while(ntext.compareTo(currentLine) != 0){
    //some condition
     }

Here i want to know what that compareto actually do. One more questin what we can use to compare two objects?

S.Hake
  • 3
  • 5
  • It does what it says on tin. What are the types of `ntext` and `currentLine`? – Boris the Spider Apr 24 '16 at 16:36
  • There is documentation that you can read.... https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#compareTo-java.lang.String- if this is a String, or the general https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html#compareTo-T- – Tunaki Apr 24 '16 at 16:36
  • 1
    @Tunaki can you think of any reason why `equals` wouldn't work? Something related to larger character sets for example? – Boris the Spider Apr 24 '16 at 16:37
  • That could be written as `while (!ntext.equals(currentLine)) {` - does that make its meaning more clear? – Andy Turner Apr 24 '16 at 16:38
  • @BoristheSpider Nope. Well except if the class is broken to begin with and doesn't have consistent `equals` and `compareTo` :). (it is *strongly recommended, but not strictly required*...) – Tunaki Apr 24 '16 at 16:39

1 Answers1

0

If those variables are strings (i assume so) it checks if they are equal, returns 0 if so, and another number if not. See the JavaDoc here: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#compareTo%28java.lang.String%29

More specifically, it goes through both strings character by character. When it finds a string of one that is not equal to the other, it returns a number representing whether the differing character is more than, less than, or equal to the corresponding character in the other string.

nhouser9
  • 6,730
  • 3
  • 21
  • 42