简体   繁体   English

扫描仪无法解析为类型

[英]Scanner cannot be resolved to a type

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.我刚刚安装了 Ubuntu 8.04 并且我正在学习 Java 课程,所以我想为什么在安装 IDE 时不安装它。 So I pick my IDE of choice, Eclipse, and I make a very simple program, Hello World, to make sure everything is running smoothly.所以我选择了我选择的 IDE Eclipse,并制作了一个非常简单的程序 Hello World,以确保一切顺利运行。 When I go to use Scanner for user input I get a very odd error:当我使用 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)

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 使用它。

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.在 Ubuntu 下,您需要将 java 编译器“javac”设置为使用 sun 的 jdk 而不是任何其他替代方案。 The difference to some of the answers posted so far is that I am talking about javac NOT java.到目前为止发布的一些答案的不同之处在于我在谈论 javac 而不是 java。 To do so fire up a shell and do the following:为此,启动一个 shell 并执行以下操作:

  1. As root or sudo type in at command line:作为 root 或 sudo 在命令行输入:

# update-alternatives --config javac

  1. Locate the number pointing to sun's jdk, type in this number, and hit "ENTER".找到指向 sun 的 jdk 的数字,输入这个数字,然后点击“ENTER”。

  2. You're done!你完成了! From now on you can enjoy java.util.Scanner under Ubuntu.从现在开始,您可以在 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.");

You imported Scanner but you're not using it.您导入了 Scanner,但您没有使用它。 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.您试图打印出一件事,但是您将程序暴露给您将使用自己的输入这一事实,因此它决定在您提供用户输入后打印“Hello World”。 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();你需要像int a=sc.nextInt();这样的东西int a=sc.nextInt(); or String b=sc.nextLine();String b=sc.nextLine(); and then give your user input.然后给你的用户输入。 But you said you want Hello World!但是你说你想要Hello World! , so Scanner is redundant. ,所以 Scanner 是多余的。

package com.company;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Input seconds: ");
        int num = in.nextInt();

        for (int i = 1; i <=num; i++) {

            if(i%10==3)
            {
                System.out.println(i);
            }
        }

    }
}

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 系统库”条目,它旁边应该有一个版本号。

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.也可能是尽管您使用的是 JDK 1.5 或更高版本,但该项目有一些特定的设置集,告诉它编译为 1.4。 You can test this via Project >> Properties >> Java Compiler and ensure the "Compiler Compliance Level" is set to 1.5 or higher.您可以通过 Project >> Properties >> Java Compiler 进行测试,并确保“Compiler Compliance Level”设置为 1.5 或更高。

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

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