[英]Java Converting String into Integer for Comparison
I am writing a compareTo
method for the Comparable Interface for a specific class. 我正在为特定类编写一个
compareTo
方法用于Comparable Interface。 I need to take two different strings and compare them and see if one holds a greater value than the other. 我需要采用两个不同的字符串并比较它们,看看是否有一个比另一个更有价值。 I would normally use
.compareTo()
but in this case it is giving me an error and not working. 我通常会使用
.compareTo()
但在这种情况下它会给我一个错误而不能正常工作。 This is what I have: 这就是我所拥有的:
public int compareTo(Object o) {
if (name < ((AccountOwnerComparable) o).name)
return -1;
else if (name > ((AccountOwnerComparable) o).name)
return 1;
else
return 0;
}
I know I can't use the < or >
but it is just to show what the scenario is. 我知道我不能使用
< or >
,只是为了表明场景是什么。
You just have to do: 你只需要这样做:
name.compareTo(String_to_be_compare);
public int compareTo(Object o) public int compareTo(Object o)
Compares this String to another Object.
将此String与另一个Object进行比较。 If the Object is a String, this function behaves like compareTo(String).
如果Object是String,则此函数的行为类似于compareTo(String)。
Note that: 注意:
Returns : 退货 :
The value 0 if the argument is a string lexicographically equal to this string;
如果参数是一个按字典顺序排列等于该字符串的字符串,则值为0 ; a value less than 0 if the argument is a string lexicographically greater than this string;
如果参数是按字典顺序大于此字符串的字符串,则小于0的值; and a value greater than 0 if the argument is a string lexicographically less than this string.
如果参数是按字典顺序小于此字符串的字符串,则值大于0 。 ( source )
( 来源 )
So to your case: 所以对你的情况:
public int compareTo(Object o)
{
AccountOwnerComparable obj = (AccountOwnerComparable) o;
return obj.name.compareTo(this.name);
}
tw: implement the comparable interface specifying what you want to compare to (usually the same type), since you did not specify anything your interface compares to anything which is odd. tw:实现类似的接口,指定你要比较的内容(通常是相同的类型),因为你没有指定任何接口与奇数的东西相比较的东西。
The Comparable
interface is generic . Comparable
接口是通用的 。 Your class declaration should probably look something like this: 你的类声明应该看起来像这样:
class AccountOwnerComparable implements Comparable<AccountOwnerComparable> {...
Notice the AccountOwnerComparable
type parameter. 请注意
AccountOwnerComparable
类型参数。 With this, you would have 有了这个,你会有
public int compareTo(AccountOwnerComparable other) {
return name.compareTo(other.name);
}
You might have missed a bracket or something while casting. 在施法时你可能错过了一个支架或什么东西。 Try the following piece of code.
尝试以下代码。
public int compareTo(Object o) {
if (name.compareTo(((AccountOwnerComparable) o).name) > 0) {
System.out.println("Greater");
return -1;
} else if (name.compareTo(((AccountOwnerComparable) o).name) < 0) {
System.out.println("Lesser");
return 1;
} else {
System.out.println("Equal");
return 0;
}
}
Use Integer.parseInt()
method. 使用
Integer.parseInt()
方法。 In this way you could use >
or <
and it is more verbose. 通过这种方式,您可以使用
>
或<
,它更详细。 I assume you want to compare two String
objects containing numbers. 我假设您要比较包含数字的两个
String
对象。 Although you did not write it in the questions, the title says so. 虽然你没有在问题中写出来,但标题却如此。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.