3

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

2
  • 3
    Although kind of silly to people who've practiced in Java...this is kind of an interesting question. Commented Dec 31, 2013 at 21:07
  • 2
    The name of your class is conflicting with the class type of variable sc that you have defined. So change the class name or alternatively use complete package name for declaring variable sc as java.util.Scanner sc = new java.util.Scanner(System.in); Commented Dec 31, 2013 at 21:10

4 Answers 4

7

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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.

Comments

2

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.