简体   繁体   中英

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.
Error 1 http://puu.sh/64VGk.jpg

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

You need to name your class something other than Scanner . That name is already taken by java.util.Scanner , and creating a new class with that name is confusing the compiler.

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 . Give the class a different name.

public class ScannerTest {
  ...
}

The unqualified use of Scanner will then point to the correct class .

You named your class 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.

Java is having trouble identifying which Scanner you are referring to. There are two Scanners in your project: java.util.Scanner and the Scanner class YOU created. Java thinks that you are referring to the Scanner class that you created, instead of the java.util.Scanner class. Simply rename your class and file to a name that java doesn't already use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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