lopping problem2
I have a problem in my program where it seems that even though the conditions for an if loop are true, it goes to the "else" anyway.
the application is quite large so i'll just post the segment which doesn't work, however if you want the full code i have no problems posting that either.
for the purpose of this, lets just say all the variables are strings. aa is "abcde", aa2 is "abcde" and theText is "abcde". I have tried the if conditions with "if theText.equals(aa) || etc..."
That doesn't work either. as you can see, in the else part i print the values of the strings. Only either aa or aa2 will have a value, not both, so one is always blank.
if((theText.compareTo(aa) < 0) || (theText.compareTo(aa2) < 0)){
System.out.println("If in loop, this message will appear, then application will exit.");
System.exit(0);
}
else{
System.out.println(aa);
System.out.println(aa2);
System.out.println(aa2.compareTo(theText));
System.out.println(theText);
JOptionPane.showConfirmDialog(null,"Are you sure you want to exit without saving?","Exit", JOptionPane.YES_NO_OPTION);
}
This is the output i get:
(blank space) in this case i had given a value to aa2.
abcde
-1
abcde
So, this confirms that both the strings are equal to each other, and the compareTo method returns -1. So tell me, why doesn't it just go to the if loop?
[1706 byte] By [
Adam123a] at [2007-11-27 5:04:30]

> aa2.compareTo(theText) is not equal to
> theText.compareTo(aa2)
>
> It's like saying since 1 is greater than 0, 0 must be
> greater than 1. Obviously incorrect.
What?
where is it saying that?, the if loop has basically "If the value returned from this compareTo method is less than 0, then do this", and the value returned is -1, so it should work?
i'm not sure what you meant by that comment.
Have you made sure there aren't any spaces in your strings? Add in a .trim(); I guess that should solve the problem.
I tried it out with these values:
theText = "abcde "; //notice the trailing space
aa = ""; //the blank you mentioned
aa2 = "abcde"; //the value you're expecting
And I got the same output as you do, so I guess it's an extra space somewhere that's messing up the comparison.
Read your own code:
if((theText.compareTo(aa) < 0) || (theText.compareTo(aa2) < 0)) {
[...]
System.out.println(aa2.compareTo(theText));
Since aa2.compareTo(theText) returns -1 as you claimed, it's highly probable that theText.compareTo(aa2) returns 1 or another positive number. Hence your if condition becomes false.
By the way, it's not a loop. Its just a statement followed by a block or other statement. No repetition.
> aa2.compareTo(theText) is not equal to
> theText.compareTo(aa2)
>
> It's like saying since 1 is greater than 0, 0 must be
> greater than 1. Obviously incorrect.
Oh, yeah! Didn't notice that :D
@ Adam, he's right. You would only get the same answer when they're equal, i.e, when it's returning 0 as the compare value. Otherwise, you'll get the opposite.
For example, if you say a.compareTo(b) ( where a and b hold the letters a and b ) then you'd get -1 since a comes before b in the alphabet.
Now if you did b.compareTo(a), you'd get 1 since b comes after.
And the line is in the else block, where're you;re printing out the values.