Introduction

Reading user input is the first step towards writing useful Java software. User input can come in many forms - mouse and keyboard interactions, a network request, command-line arguments, files that are updated with data relevant for a program’s execution, etc.

We’re going to focus on keyboard input via something called the standard input stream. You may recognize it as Java’s System.in.

We’re going to use Scanner class to make our interaction with the underlying stream easier. Since Scanner has some downfalls, we’ll also be using the BufferedReader and InputStreamReader classes to process the System.in stream.

In the end, we’ll Decorate the InputStream class and implement our own custom UncloseableInputStream to handle issues with the Scanner class.

Java Scanner Class

The java.util.Scanner class is a simple scanner that can parse and handle primitive inputs, Strings and streams. Since System.in is just an InputStream, we can construct a Scanner as such:

Scanner sc = new Scanner(System.in);

This Scanner instance can now scan and parse booleans, integers, floats, bytes and Strings.

Let’s see how we can extract information from a Scanner into variables we can work with:

Scanner sc = new Scanner(System.in);

// Read an integer into a variable
int myInteger = sc.nextInt();

// Read a byte into a variable
byte myByte = sc.nextByte();

// Read a line until newline or EOF into a string
String myLine = sc.nextLine();

// Closing the scanner
sc.close();

Again, the constructor doesn’t have to take System.in. It can take any FileInputStreamReadableReadableByteChannelPath (of a file to read), or even String. Additionally, as the second argument, it can specify a character encoding to interpret said characters by:

Scanner sc = new Scanner(new FileInputStream("myFile.txt"), "UTF-8");

Note that Scanner has to be closed when you’re done working with it. The easiest way to do this is via the try-with-resources statement.

#java

How to Get User Input in Java
79.00 GEEK