Reputation: 53
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. :)
Upvotes: 5
Views: 25067
Reputation: 124225
parseInt
is static method of Integer class. To invoke it you have to do one of few things:
invoke it on Integer class
y = Integer.parseInt(s, 5)
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);
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);
Upvotes: 1
Reputation: 15641
Your are trying to access a static method of the Integer
class
You must do:
y = Integer.parseInt(s,5);
Upvotes: 1
Reputation: 46408
parseInt is a static method in Integer class. you need to call it like this:
y = Integer.parseInt(s,5);
Upvotes: 5