eclipse 不能使用 Scanner 类,构造函数未定义,方法未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17462795/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 20:50:59  来源:igfitidea点击:

Can't use Scanner class, constructor is undefined, method is undefined

javaeclipse

提问by user2549089

When i want import scanner class in my project eclipse show me some errore :

当我想在我的项目 eclipse 中导入扫描仪类时,向我显示一些错误:

 Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
  The constructor Scanner(InputStream) is undefined
  The method nextLine() is undefined for the type Scanner
 Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
  The constructor Scanner(InputStream) is undefined
  The method nextLine() is undefined for the type Scanner

and this is my code :

这是我的代码:

import java.util.Scanner;

public class Scanner {

    public static void main(String[] args) {

        Scanner myScanner = new Scanner(System.in);
        System.out.println(myScanner.nextLine());

    }

}

回答by Jon Skeet

The problem is that you're also declaringa class called Scanner. That means that when you then declare a variable of type Scannerand try to call the constructor, the compiler thinks you're talking about yourclass. Just change your own class to something else (e.g. Test):

问题是您还声明了一个名为Scanner. 这意味着当你声明一个类型的变量Scanner并尝试调用构造函数时,编译器认为你在​​谈论你的类。只需将您自己的类更改为其他内容(例如Test):

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner myScanner = new Scanner(System.in);
        System.out.println(myScanner.nextLine());
    }
}

Alternatively you couldjust fully-qualify the name when you mean java.util.Scanner- but this would be a bad idea in terms of readability.

或者,您可以在您的意思时完全限定名称java.util.Scanner- 但就可读性而言,这将是一个坏主意。

// Please don't do this - but it would work.
public class Scanner {
    public static void main(String[] args) {
        java.util.Scanner myScanner = new java.util.Scanner(System.in);
        System.out.println(myScanner.nextLine());
    }
}

回答by Kai

Try

尝试

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

instead. Else the compiler tries to instantiate your class which is also called Scanner. Or just rename your own Scannerclass to something else.

反而。否则编译器会尝试实例化您的类,该类也称为Scanner. 或者只是将您自己的Scanner类重命名为其他名称。

回答by gjman2

Try to change your class name eg:

尝试更改您的班级名称,例如:

import java.util.Scanner;

public class ScannerTest {

    public static void main(String[] args) {

        Scanner myScanner = new Scanner(System.in);
        System.out.println(myScanner.nextLine());

    }

}

回答by MohamedSanaulla

Please change the name of your class:

请更改您的班级名称:

public class Scanner {

to some other name. The compiler is unable to see Scanner as java.util.Scannerbecause it sees it as your class (which doesn't have such constructor nor method so it gives you errors informing about it).

到其他名称。编译器无法看到 Scanner ,java.util.Scanner因为它将它视为您的类(它没有这样的构造函数或方法,因此它会给您错误通知)。

回答by Manish Doshi

You should give class name as different then API classes of Java.So just change class name from Scannerto ScannerProgram.

您应该将类​​名指定为与 Java 的 API 类不同的类名。因此只需将类名从 更改ScannerScannerProgram

import java.util.Scanner;

     public class ScannerProgram {

    public static void main(String[] args) {

        Scanner myScanner = new Scanner(System.in);
        System.out.println(myScanner.nextLine());

    }

}