I am wondering which is the best way to go with java code. I need to create a class with simple prompts for input.. I have tried using both classes and cannot work out the particular benefits for each. Is this because I am still in the early stages of programming or are there situations that will occur as it becomes more complex??
import java.util.Scanner;
public class myClass
{
Scanner stdin = new Scanner(System.in);
public String getInput(String prompt)
{
System.out.print(prompt);
return stdin.nextLine();
}
}
… or
import java.io.*;
public class myClass
{
public static void main(String[] args) throws IOException
{
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Input something: ");
String name = stdin.readLine();
}
}
I know these examples are showing different methods within these classes, but thought this might serve well for the discussion.
I’m really not sure which site is the best to ask this on.
9
I’ve been a Java programmer for 6 years now, and I rarely have to worry about how to read input from the command line. Most times, when I’m dealing with input and output, they are usually streams of some sort and therefore using a BufferedReader
is generally a best practice, since it takes the bottleneck off having to read a file line by line.
However, for what you’re out to do, using System.in
with Scanner
is perfectly fine. Lets just say that if I wanted to handle large amounts of input, perhaps I would wrap System.in
with a BufferedInputStream
for efficiency, but if your program is waiting on you, then there is no advantage gained.
6
Simply reading System.in with BufferedReader, you end up writing a few lines of boilerplate code.
Moving up to Scanner, as suggested in Neil’s answer, makes it easier to pull data of various types out of the stream.
Personally, when a command-line interface is really the best choice (which includes any time it’s a stated requirement), I like to use System.console(). It’s not always available (for example, when running inside some IDEs), but when it is, it offers useful things like readPassword() which prevents the user’s input from being echoed back to the screen, and it makes it so that you can read from and write to the console using the same object. It’s really designed around the typical use case for command line tools that require user interaction.
Using System.console() doesn’t explicitly conflict with Scanner:
Console console = System.console();
String name = console.readLine("Please enter your name: ");
Scanner ageScanner = new Scanner(console.readLine("How old are you? ");
int age = ageScanner.nextInt();
They don’t quite play perfectly together in this way, since you have to instantiate a new Scanner for each line. Console does offer access to a Reader, which you can use to instantiate a Scanner:
Console console = System.console();
String name = console.readLine("Please enter your name: ");
Scanner consoleScanner = new Scanner(console.reader());
int age = consoleScanner.nextInt();
(Disclaimer: I haven’t tried the above code, and I’m not exactly sure how things will behave if you use console.readLine() intermixed with usages of a Scanner made from console.reader(). For clarity and consistency, it might be wise to choose one method and stick to it.)
Sticking with this example, I’d be more likely to write:
Console console = System.console();
String name = console.readLine("Please enter your name: ");
int age = Integer.parseInt(console.readLine("How old are you? ");
Depending on your needs, you may find that one, the other, or both are best suited to your needs. Generally, I find that Scanner is better suited to reading a file with a known format, and Console is better suited to interactive user input, because Scanner really shines when you have multiple tokens per line of input. Asking a user to enter that sort of input interactively is probably not going to result in the best experience.
JavaDoc: java.io.Console
Related question: Java: How to get input from System.console()
1