简体   繁体   English

Java CompareTo没有返回好的结果

[英]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. 我已经实现了一种功能,用于检查一个网址是否比另一个网址新,以便遍历整个网站,例如: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! 但是当我在Java中使用compareTo函数时,在某些情况下不会返回好的结果,请检查一下,请帮忙!

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 :( 当我比较page = 8和page = 9时,它可以正常工作,但是例如在上面(page = 9和page = 10)时,它不起作用:(

That is because 1 (the first digit of 11 ) is smaller than 9 . 这是因为111的第一个数字)小于9 If you use 09 and 11 it will work. 如果您使用0911 ,它将起作用。 In order to make it work, extract the number and compare the number by comparing integers, instead of characters. 为了使其工作,请提取数字并通过比较整数而不是字符来比较数字。

That is because 9 is bigger than 1. 这是因为9大于1。

You need to substring the part after "=" and cast it to int, then compare. 您需要在“ =”之后对部分进行子串化并将其转换为int,然后进行比较。

Simple stuff. 简单的东西。 9 > 1. Parse the URL until =. 9> 1.解析URL直到=。 Cast that char to Int. 将该字符转换为Int。 Then, do the same thing. 然后,做同样的事情。 you'd get your desired output! 您将获得所需的输出!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM