eclipse 扫描仪无法解析为类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/79538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scanner cannot be resolved to a type
提问by Austin
I just installed Ubuntu 8.04 and I'm taking a course in Java so I figured why not install a IDE while I am installing it. So I pick my IDE of choice, Eclipse, and I make a very simple program, Hello World, to make sure everything is running smoothly. When I go to use Scanner for user input I get a very odd error:
我刚刚安装了 Ubuntu 8.04 并且我正在学习 Java 课程,所以我想为什么在安装 IDE 时不安装它。所以我选择了我选择的 IDE,Eclipse,我制作了一个非常简单的程序 Hello World,以确保一切顺利运行。当我使用 Scanner 进行用户输入时,我收到一个非常奇怪的错误:
My code:
我的代码:
import java.util.Scanner;class test { public static void main (String [] args) { Scanner sc = new Scanner(System.in); System.out.println("hi"); } }
The output:
输出:
Exception in thread "main" java.lang.Error: Unresolved compilation problems: Scanner cannot be resolved to a type Scanner cannot be resolved to a type at test.main(test.java:5)
回答by Thomas
The Scanner class is new in Java 5. I do not know what Hardy's default Java environment is, but it is not Sun's and therefore may be outdated.
Scanner 类是 Java 5 中的新类。我不知道 Hardy 的默认 Java 环境是什么,但它不是 Sun 的,因此可能已经过时。
I recommend installing the package sun-java6-jdk to get the most up-to-date version, then telling Eclipse to use it.
我建议安装包 sun-java6-jdk 以获得最新版本,然后告诉 Eclipse 使用它。
回答by tgdavies
If you are using a version of Java before 1.5, java.util.Scanner doesn't exist.
如果您使用的是 1.5 之前的 Java 版本,则 java.util.Scanner 不存在。
Which version of the JDK is your Eclipse project set up to use?
您的 Eclipse 项目设置使用哪个版本的 JDK?
Have a look at Project, Properties, Java Build Path -- look for the 'JRE System Library' entry, which should have a version number next to it.
查看项目、属性、Java 构建路径——查找“JRE 系统库”条目,它旁边应该有一个版本号。
回答by Lee Theobald
It could also be that although you are have JDK 1.5 or higher, the project has some specific settings set that tell it to compile as 1.4. You can test this via Project >> Properties >> Java Compiler and ensure the "Compiler Compliance Level" is set to 1.5 or higher.
也可能是尽管您使用的是 JDK 1.5 或更高版本,但该项目有一些特定的设置集,告诉它编译为 1.4。您可以通过 Project >> Properties >> Java Compiler 进行测试,并确保“Compiler Compliance Level”设置为 1.5 或更高。
回答by Lee Theobald
I know, It's quite a while since the question was posted. But the solution may still be of interest to anyone out there. It's actually quite simple...
我知道,这个问题发布已经有一段时间了。但该解决方案可能仍然对那里的任何人感兴趣。其实很简单...
Under Ubuntu you need to set the java compiler "javac" to use sun's jdk instead of any other alternative. The difference to some of the answers posted so far is that I am talking about javac NOT java. To do so fire up a shell and do the following:
在 Ubuntu 下,您需要将 java 编译器“javac”设置为使用 sun 的 jdk 而不是任何其他替代方案。与到目前为止发布的一些答案的不同之处在于我在谈论 javac 而不是 java。为此,启动一个 shell 并执行以下操作:
- As root or sudo type in at command line:
- 作为 root 或 sudo 在命令行输入:
# update-alternatives --config javac
# update-alternatives --config javac
Locate the number pointing to sun's jdk, type in this number, and hit "ENTER".
You're done! From now on you can enjoy java.util.Scanner under Ubuntu.
找到指向 sun 的 jdk 的数字,输入这个数字,然后点击“ENTER”。
你完成了!从现在开始,您可以在 Ubuntu 下享受 java.util.Scanner 了。
System.out.println("Say thank you, Mr.");
Scanner scanner = java.util.Scanner(System.in);
String thanks = scanner.next();
System.out.println("Your welcome.");
System.out.println("Say thank you, Mr.");
Scanner scanner = java.util.Scanner(System.in);
String thanks = scanner.next();
System.out.println("Your welcome.");
回答by boi yeet
You imported Scanner but you're not using it. You're using Scanner, which requires user inputs. You're trying to print out one thing, but you're exposing the your program to the fact that you are going to use your own input, so it decides to print "Hello World" after you give a user input. But since you are not deciding what the program will print, the system gets confused since it doesn't know what to print. You need something like int a=sc.nextInt();
or String b=sc.nextLine();
and then give your user input. But you said you want Hello World!
, so Scanner is redundant.
您导入了扫描仪,但您没有使用它。您正在使用扫描仪,它需要用户输入。您试图打印出一件事,但是您将程序暴露给您将使用自己的输入这一事实,因此它决定在您提供用户输入后打印“Hello World”。但是由于您没有决定程序将打印什么,系统会因为不知道要打印什么而感到困惑。你需要类似int a=sc.nextInt();
或的东西String b=sc.nextLine();
,然后给你的用户输入。但是你说你想要Hello World!
,所以 Scanner 是多余的。