简体   繁体   English

Java - 使用Scanner编译错误

[英]Java - compilation errors using Scanner

This is my current class: 这是我目前的课程:

package Mathias;

import java.util.*;

public class Scanner {
    public static void main(String args[]) {
        System.out.print("What's your name?");
        Scanner sc = new Scanner(System.in);
        String Input = sc.nextLine();
        System.out.println("Hello, " + Input + ".");
    }
}

I get two errors on the 5th & 6th lines. 我在第5和第6行遇到两个错误。
Error 1 http://puu.sh/64VGk.jpg 错误1 http://puu.sh/64VGk.jpg

Error 2 http://puu.sh/64VHe.jpg 错误2 http://puu.sh/64VHe.jpg

You need to name your class something other than Scanner . 您需要将您的课程命名为Scanner以外的其他课程。 That name is already taken by java.util.Scanner , and creating a new class with that name is confusing the compiler. 该名称已由java.util.Scanner ,并且使用该名称创建新类会使编译器感到困惑。

Alternatively, you could try specifying: 或者,您可以尝试指定:

java.util.Scanner sc = new java.util.Scanner(System.in);

so that your code is unambiguous. 这样你的代码就是明确的。

Your class hides the definition of the built-in class java.util.Scanner and doesnt have a constructor that accepts an InputStream . 您的类隐藏了内置类java.util.Scanner的定义,并且没有接受InputStream的构造函数。 Give the class a different name. 给这个班一个不同的名字。

public class ScannerTest {
  ...
}

The unqualified use of Scanner will then point to the correct class . 然后,不合格使用Scanner将指向正确的类

You named your class Scanner. 您将您的班级命名为Scanner。 Name it something else, then add 将其命名为其他内容,然后添加

import java.util.Scanner;

to your imports. 你的进口。 Instead of accessing the library Scanner class you are trying to access your own class- which doesn't have any of the functionality you are trying to use. 您正在尝试访问自己的类 - 而不是访问库Scanner类 - 它没有您尝试使用的任何功能。

Java is having trouble identifying which Scanner you are referring to. Java无法识别您所指的扫描仪。 There are two Scanners in your project: java.util.Scanner and the Scanner class YOU created. 项目中有两个扫描程序:java.util.Scanner和您创建的Scanner类。 Java thinks that you are referring to the Scanner class that you created, instead of the java.util.Scanner class. Java认为您指的是您创建的Scanner类,而不是java.util.Scanner类。 Simply rename your class and file to a name that java doesn't already use. 只需将您的类和文件重命名为java尚未使用的名称即可。

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

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