cannot find symbol: parseInt
Asked Answered
C

3

5

I have allready compiled a few tiny programms in java and everything was fine. But my new code has any problem.

class myclass
{
 public static void main (String[] args)
 {
  int x, y;
  String s ="sssss";
  x=args.length;
  s= args[0].substring(1,2);
  System.out.println("Num of args: "+x);
  System.out.println("List of args:");
  y = parseInt(s,5);
  }
}

The compiler says:

 e:\java\4>javac myclass.java 
  myclass.java:11: error: cannot find symbol   
   y = parseInt(s,5);
       ^   symbol:   method parseInt(String,int)   location: class myclass 1 error

 e:\java\4>

The strange thing is that the compiler jumps over the method substring (as there is no problem) but the method parseInt seems to have any problem. y = parseInt(s); OR: y = parseInt("Hello"); --> Also the same compiler message.

Or does the method not exist? docs.oracle-->Integer says it exists :)

It makes my really crazy as i don't know how to search for the error. I have checked the internet allready, i checked the classpath and the path...

So it would be great if any expert could help me. :)

Cameraman answered 17/11, 2012 at 23:6 Comment(1)
Java's Integer.parseInt has an optional radix parameter. But make sure you want a radix of 5. If you are trying to parse "regular" number inputs, you can just ommit the radix completely.Fragonard
R
5

parseInt is a static method in Integer class. you need to call it like this:

  y = Integer.parseInt(s,5);
Risser answered 17/11, 2012 at 23:8 Comment(1)
Thanks for the quick reply, i think alone i didn't get it by the end of this year.Cameraman
M
1

Your are trying to access a static method of the Integer class

You must do:

y = Integer.parseInt(s,5);
Marmara answered 17/11, 2012 at 23:8 Comment(0)
H
1

parseInt is static method of Integer class. To invoke it you have to do one of few things:

  1. invoke it on Integer class

     y = Integer.parseInt(s, 5)
    
  2. invoke it on variable of Integer type (but this way is discouraged since such code still will be compiled into Integer.parseInt but will look like parseInt is instance method - so non-static which is not true and may cause confusion)

     Integer i = null;//yes reference can be even null in this case
     y = i.parseInt(s, 5);
    
  3. import that static method using static import via import static java.lang.Integer.parseInt;. This way you can use that method directly:

     y = parseInt(s,5);
    
Hillhouse answered 17/11, 2012 at 23:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.