简体   繁体   中英

Error in Simple Scanner java Program in netbeans java

Program:

package scanner;

import java.util.*;

public class Scanner {

    public static void main(String[] args)throws Exception {
        System.out.println("Enter your name ");
        Scanner scanner = new Scanner(System.in);
        String data=scanner.nextLine();
        System.out.println(data);
    }

}

Error/output:

run:
Enter your name

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - constructor Scanner in class scanner.Scanner cannot be applied to given types;
  required: no arguments
  found: java.io.InputStream
  reason: actual and formal argument lists differ in length
    at scanner.Scanner.main(Scanner.java:10)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)

Don't name your class Scanner as this causes a naming conflict with the java.util.Scanner! Give your class a different name such as ScannerTest . As a general rule, you will want to avoid giving your classes names that conflict with key or common core Java classes.

You have named your class Scanner , so referring to the simple name Scanner in your code refers to your own class (which doesn't have a constructor that takes an InputStream ), not java.util.Scanner .

Either use the fully qualified name java.util.Scanner or rename your class to something other than a built-in Java class name.

System.out.println("Enter your name ");
java.util.Scanner scanner = new java.util.Scanner(System.in);
String data=scanner.nextLine();
System.out.println(data);

Here the name of your class and java.util.Scanner are same. Java tries to match with your constructor. By default empty constructor will be added. There is no argument constructor matches in your code.

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