简体   繁体   English

无法将字符串转换为int(Java)

[英]Can't convert string to int (Java)

For some reason javac can't compile Integer.parseInt(). 由于某种原因,javac无法编译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. 我尝试了几个包含parseInt的程序并且它们不起作用所以我编写了一个简单的小程序来测试问题是parseInt还是程序中的其他东西。 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();" 即使使用上面的程序,我仍然遇到编译错误:“错误:找不到符号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. 我尝试使用Double.parseDouble()运行该程序,它工作得很好。 Any ideas as to why I'm getting this error? 有关为什么我收到此错误的任何想法?

Java imports the java.lang package for you. Java为您导入java.lang包。 All the classes in that package are therefore available to your program without needing to use an import statement. 因此,该程序包中的所有类都可用于您的程序,而无需使用import语句。 If however you have declared a class called Integer in your package (the default package), then you are shadowing the Integer in java.lang . 但是,如果您在包中声明了一个名为Integer的类(默认包),那么您将在java.lang隐藏 Integer

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. 或者在自定义Integer类中创建自己的static parseInt(String)方法。 That will make the program compile. 这将使程序编译。 Make sure that it does what you want. 确保它符合您的要求。

Instead of Integer.parseInt() , try: 而不是Integer.parseInt() ,尝试:

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. 这将确保您在java.lang中确实使用正确的Integer类 - 如果这不起作用,那么您的环境就会出现严重问题。 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. 如果确实如此,那么你必须在某个地方有另一个名为Integer类,而不是java.lang.Integer类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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