1

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!

3
  • 3
    Beacause you are calling parseInt directly, You must use Integer.parseInt() as it it is an static method of Integer class Commented Aug 9, 2011 at 5:36
  • what is Address? does it have a method parseInt()? Commented Aug 9, 2011 at 5:36
  • If you wanted to, you could statically import the parseInt method. Commented Aug 9, 2011 at 5:37

7 Answers 7

4

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...
}
Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

2

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())

Comments

1

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.

Comments

0

if(Integer.parseInt(getStreetNumber())

Comments

0

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.

Comments

0

Yes, you can also statically import it:

import static java.lang.Integer.parseInt;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.