Reputation: 29
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
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
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
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"
.
Upvotes: 2