Scott Wayne
Scott Wayne

Reputation: 29

Java import not working

I'm very new to Java, and this is probably a very dumb question. I'm trying to get a simple input from the user, and to do that I get the Scanner class, or BufferedReader. Yet when I try to import java.io.*, the classes show up undefined.

Here's my code:

package testing;

import java.io.*;
import java.util.Scanner;

public class Something {
    public static void logln(String content) {
        System.out.println(content);
    }
    public static void main(String [] args) {

    }
    void getInput(String prompt) {
        Scanner s = new Scanner();
    }
}

Scanner s is showing up undefined. Why might this be?

Upvotes: 0

Views: 1390

Answers (3)

Sarit Adhikari
Sarit Adhikari

Reputation: 1402

Your problem has nothing to do with java import.

If you want to take input from inputStream which is typically connected to keyboard input, change your constructor to

Scanner sc = new Scanner(System.in);

Read the entered input using,

String content = sc.nextLine();

Upvotes: 0

neoprogrammer
neoprogrammer

Reputation: 91

Hovercraft Full Of Eels, has pointed to the correct error. I would like to add a couple of things - the correct constructor would be

Scanner s = new Scanner(System.in); 

Also I don't quite understand why your getter method, getInput() is parametrized. Would you like to elaborate on that?

Upvotes: 0

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285450

Here's your problem:

Scanner s = new Scanner();  // no constructor exists

You need to pass a parameter into the Scanner constructor as this class does not have a default no-parameter constructor. You will want to read the error message critically as it will often tell you exactly what is wrong, here that "The constructor Scanner() is undefined".

  • If you have similar questions in the future, always post the exact and complete error message.
  • Also, get real friendly with the Java API as it will help you understand the classes that you're using. Here the Scanner API will tell you exactly what constructors are available for this class.

Upvotes: 2

Related Questions