简体   繁体   中英

Can't convert string to int (Java)

For some reason javac can't compile Integer.parseInt(). I tried several programs that have parseInt in them and they don't work so I wrote up a simple little program to test whether the problem is parseInt or something else in the programs. Here's the test program I wrote:

public class Test
{
public static void main(String[] args)
{
int a = Integer.parseInt(args[0]);  
System.out.println(a);

}
}

Even with the program above, I still got a compiler error saying: "error: cannot find symbol a = Integer.parseInt();" with an arrow point to the . between Integer and parse. I've tried running the program with Double.parseDouble() and it works just fine. Any ideas as to why I'm getting this error?

Java imports the java.lang package for you. All the classes in that package are therefore available to your program without needing to use an import statement. If however you have declared a class called Integer in your package (the default package), then you are shadowing the Integer in java.lang .

You can use the fully qualified name of the class instead

java.lang.Integer.parseInt(args[0]);

Or create your own static parseInt(String) method in your custom Integer class. That will make the program compile. Make sure that it does what you want.

Instead of Integer.parseInt() , try:

java.lang.Integer.parseInt(args[0]);

This will ensure you definitely use the correct Integer class in java.lang - if that doesn't work then there's something seriously wrong with your environment. If it does then you must have another class called Integer somewhere that you're implicitly referring to instead of the java.lang.Integer class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM