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

后端 未结 5 1876
野趣味
野趣味 2020-12-04 00:12

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

 Exception in thread \"main\" java.lang.Error: Unresolved compilati         


        
5条回答
  •  北荒
    北荒 (楼主)
    2020-12-04 00:28

    The problem is that you're also declaring a class called Scanner. That means that when you then declare a variable of type Scanner and try to call the constructor, the compiler thinks you're talking about your class. Just change your own class to something else (e.g. 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 could just fully-qualify the name when you mean java.util.Scanner - but this would be a bad idea in terms of readability.

    // 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());
        }
    }
    

提交回复
热议问题