Java parseInt() 问题

发布于 2024-11-28 17:34:20 字数 608 浏览 2 评论 0原文

为什么当我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

晨敛清荷 2024-12-05 17:34:20

这是因为 parseInt 是属于 Integer 类的方法:您必须调用它 Integer.parseInt(value);。我非常怀疑您的自定义类(我怀疑这都是 Address 类的一部分?)或 tempAddress 实例中是否有 parseInt 函数。

试试这个:

public int compareTo(Object o) 
{
    Address tempAddress = (Address)o;
    if(Integer.parseInt(getStreetNumber()) < 
          // you need to parse the return value of tempAddress's getStreetNumber() 
          // not get the tempAddress's parseInt of this.getStreetNumber()
          Integer.parseInt(tempAddress.getStreetNumber()))
    {
        return -1;
    }
 // etc...
}

That is because parseInt is a method which belongs to the Integer class: you have to call it Integer.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:

public int compareTo(Object o) 
{
    Address tempAddress = (Address)o;
    if(Integer.parseInt(getStreetNumber()) < 
          // you need to parse the return value of tempAddress's getStreetNumber() 
          // not get the tempAddress's parseInt of this.getStreetNumber()
          Integer.parseInt(tempAddress.getStreetNumber()))
    {
        return -1;
    }
 // etc...
}
老街孤人 2024-12-05 17:34:20

因为您调用的是 parseInt() 而不是 Integer.parseInt()

Because you called parseInt() not Integer.parseInt()

无需解释 2024-12-05 17:34:20

parseIntInteger 类。你称其为
Integer.parseInt(the_string_you_want_to_parse)

如果您想解析另一个类中的方法返回的字符串,您可以将其称为
Integer.parseInt(tempAddress.getStreetNumber())

parseInt is a static method in the Integer class. You call it as
Integer.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())

赏烟花じ飞满天 2024-12-05 17:34:20

其他人回答了你的直接问题,但我要说的是,这是一种非常干净的方式来完成你想要做的同样的事情:

return Integer.valueOf(getStreetNumber())
    .compareTo(
       Integer.valueOf(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:

return Integer.valueOf(getStreetNumber())
    .compareTo(
       Integer.valueOf(tempAddress.getStreetNumber()));

Integer already implements Comparable, so you might as well leverage it.

七颜 2024-12-05 17:34:20

if(Integer.parseInt(getStreetNumber())

if(Integer.parseInt(getStreetNumber())

风为裳 2024-12-05 17:34:20

如何在地址引用上调用 parseInt ???

  • > if(parseInt(getStreetNumber()) < tempAddress.parseInt(getStreetNumber()))

另外你应该总是说Integer.parseInt(),你不能直接调用parseInt。

How can you call a parseInt on a Address reference???

  • > if(parseInt(getStreetNumber()) < tempAddress.parseInt(getStreetNumber()))

Also you should always say Integer.parseInt(), you cant call the parseInt directly.

场罚期间 2024-12-05 17:34:20

是的,您也可以静态导入它:

import static java.lang.Integer.parseInt;

Yes, you can also statically import it:

import static java.lang.Integer.parseInt;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文