Java parseInt() 问题
为什么当我使用 parseInt 时:
private String certainNumber;
public int getNumber()
{
return Integer.parseInt(certainNumber);
}
它会编译。
但是如果我这样做:
public String getStreetNumber()
{
return streetNumber;
}
并像这样解析返回值:
@Override
public int compareTo(Object o)
{
Address tempAddress = (Address)o;
if(Integer.parseInt(getStreetNumber()) < tempAddress.Integer.parseInt(getStreetNumber()))
{
return -1;
}
... // etc.
}
它无法编译?
编辑:尝试了建议...仍然没有编译?
edit2:感谢大家的帮助!
Why is it that when I use parseInt for this:
private String certainNumber;
public int getNumber()
{
return Integer.parseInt(certainNumber);
}
It compiles.
But If I were to do this:
public String getStreetNumber()
{
return streetNumber;
}
and parseInt the returned value like so:
@Override
public int compareTo(Object o)
{
Address tempAddress = (Address)o;
if(Integer.parseInt(getStreetNumber()) < tempAddress.Integer.parseInt(getStreetNumber()))
{
return -1;
}
... // etc.
}
It does not compile?
edit: tried the suggestions... still not compiling?
edit2: Thanks for the help guys!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这是因为
parseInt
是属于Integer
类的方法:您必须调用它Integer.parseInt(value);
。我非常怀疑您的自定义类(我怀疑这都是 Address 类的一部分?)或 tempAddress 实例中是否有 parseInt 函数。试试这个:
That is because
parseInt
is a method which belongs to theInteger
class: you have to call itInteger.parseInt(value);
. I highly doubt that you have a parseInt function in either your custom class (I suspect that this is all part of an Address class?) or the tempAddress instance.Try this:
因为您调用的是
parseInt()
而不是Integer.parseInt()
Because you called
parseInt()
notInteger.parseInt()
parseInt
是Integer
类。你称其为Integer.parseInt(the_string_you_want_to_parse)
如果您想解析另一个类中的方法返回的字符串,您可以将其称为
Integer.parseInt(tempAddress.getStreetNumber())
parseInt
is a static method in theInteger
class. You call it asInteger.parseInt(the_string_you_want_to_parse)
If you want to parse an string returned by a method in another class you call it as
Integer.parseInt(tempAddress.getStreetNumber())
其他人回答了你的直接问题,但我要说的是,这是一种非常干净的方式来完成你想要做的同样的事情:
Integer
已经实现了Comparable
,所以你不妨利用它。Others answered your direct question, but let me say that this is a very clean way of doing the same thing you're trying to do:
Integer
already implementsComparable
, so you might as well leverage it.if(Integer.parseInt(getStreetNumber())
if(Integer.parseInt(getStreetNumber())
如何在地址引用上调用 parseInt ???
另外你应该总是说Integer.parseInt(),你不能直接调用parseInt。
How can you call a parseInt on a Address reference???
Also you should always say Integer.parseInt(), you cant call the parseInt directly.
是的,您也可以静态导入它:
Yes, you can also statically import it: