prk
prk

Reputation: 3809

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

Upvotes: 3

Views: 414

Answers (4)

Daisy Holton
Daisy Holton

Reputation: 519

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.

Upvotes: 0

Reimeus
Reimeus

Reputation: 159754

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.

Upvotes: 4

Paul Becotte
Paul Becotte

Reputation: 9977

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.

Upvotes: 2

Bill the Lizard
Bill the Lizard

Reputation: 405735

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.

Upvotes: 7

Related Questions