user2762881
user2762881

Reputation: 77

Java CompareTo not returning good results

I've implemented a function to check if one url is newer that the other one, in order to go through whole website ex: page1, page2, page3.

But when i'm using compareTo function in Java it doesn't return good result in some case check it out and Please help!

String someString1 = "http://view-inventory.aspx?_page=9";
String someString2 = "http://view-inventory.aspx?_page=11";

int comparisonResult = someString2.compareTo(someString1);

System.out.println("Comparison result:"+comparisonResult);


if(comparisonResult==0){
    System.out.println("We're equal");
}
else if(comparisonResult>0){
    System.out.println("Some one is bigger");
}
else {
    System.out.println("Some one is smaller");
}

when i compare page=8 and page=9 it works fine but for example above(page=9 and page=10) it doesn't work :(

Upvotes: 0

Views: 113

Answers (3)

VictorCreator
VictorCreator

Reputation: 744

Simple stuff. 9 > 1. Parse the URL until =. Cast that char to Int. Then, do the same thing. you'd get your desired output!

Upvotes: 0

weiglt
weiglt

Reputation: 1079

That is because 9 is bigger than 1.

You need to substring the part after "=" and cast it to int, then compare.

Upvotes: 0

Martijn Courteaux
Martijn Courteaux

Reputation: 68887

That is because 1 (the first digit of 11) is smaller than 9. If you use 09 and 11 it will work. In order to make it work, extract the number and compare the number by comparing integers, instead of characters.

Upvotes: 1

Related Questions