I am trying to compare two words and see if guess comes before word using compareTo()
while(!guess.equals(word)){
if(compare < 0){
System.out.println(word " Comes before your guess. ");
detail();
} else if(compare > 0) {
System.out.println(word "Comes after your guess. ");
detail();
}
}
System.out.println("You guessed it!");
CodePudding user response:
s1.compareTo(s2)
is
- positive(>0) if
s1
comes afters2
, - negative(<0) if
s1
comes befores2
, - zero (0) if
s1
ands2
coincide
So, in your case you should
if(word.compareTo(guess) < 0){
System.out.println(word " Comes before your guess. ");
detail();
} else if(word.compareTo(guess) > 0) {
System.out.println(word "Comes after your guess. ");
detail();
}