Apps/Gaming

Getting User Input in Java

As mentioned in the recent Java Output Basics programming tutorial, the keyboard is typically the standard input device and the screen is the standard output device. Having covered how to display information on the screen in Java in that article, it is time to turn our attention to accepting user6 6input via the keyboard. Java provides three ways to read input from the user in a command-line environment: Buffered Reader clet, scanner class, and Console class. We will therefore explore each of these classes in detail with code examples.

If you missed our previous Java tutorial, you can read it here: Java Output Basics.

What is the BufferedReader Class in Java

Extending the Readers class, BufferedReader exposes methods to read all sorts of character sequences – from a single character to an array of characters, as well as a readLine() method which reads a whole line. Meanwhile, the InputStreamReader class converts an input stream of bytes into a stream of characters so that it can be read by BufferedReaderas it expects a stream of characters.

Here is how to create an instance of the BufferedReader class to take input from the user in Java:

// System.in refers to the standard input, which is the keyboard by default. BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

How to Read Input as a String using a BufferedReader in Java

Here is a code example showing how to complete a class that prompts the user for his/her name and then greets him/her using BufferReader in Java:

import java.io.BufferedReader; import java.io.InputStreamReader; public class BufferedReaderExample { public static void main(String args[]){ // Instantiate the BufferedReader class BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); // Prompt the user for input System.out.println(“Enter your name: “); try { // Read the name as a String String name = input.readLine(); // Greet the user by name System.out.println(“Hello ” + name + “.”); } catch (Exception e) { // print the exception to the console System.out.println(“Something went wrong!”); } } }

Reading: Top Online Courses to Learn Java

How to use the Scanner Class in Java

In the first example, programmers saw how to read a thong from the standard input. Added in the Java 1.5 release, the scanner class can read formatted input and has several methods for different types of data. the scanner class is considered to be much easier to use than BufferedReader because it does not throw any exceptions and can read directly from the standard input.

Just look how simple it is to instantiate the scanner classes in Java:

Scanner input = new Scanner(System.in);

Reading Input as a String Using a Scanner in Java

Here is a class that is virtually identical to the one above except that it utilizes the scanner class to read the user input in Java:

import java.util.Scanner; public class ScannerStringExample { public static void main(String args[]){ // Instantiate the Scanner class Scanner input = new Scanner(System.in); // Prompt the user for input System.out.println(“Enter your name: “); // Read the name as a String String name = input.nextLine(); // Greet the user by name System.out.println(“Your name is: “+name); } }

Notice that the scanner class is found in the java.util package and not in java.io.

How to Read Input as an Integer Using a Scanner in Java

As mentioned above, the scanner class provides a number of methods for different types of data. Let’s take a look at how to use a scanner to take an int value as an input from the user in Java:

import java.util.Scanner; public class ScannerIntegerExample { public static void main(String args[]){ // Instantiate the Scanner class Scanner input = new Scanner(System.in); // Prompt the user for input System.out.println(“Enter your age: “); // Read the age as an Integer Integer age = input.nextInt(); // Confirm the user’s age System.out.println(“Your age is: “+age); } }

Other than nextLine()which is used to read thongsthe scanner method names are fairly intuitive:

  • nextBoolean(): Reads a boolean value from the user
  • nextByte(): Reads a byte value from the user
  • nextDouble(): Reads a double value from the user
  • nextFloat(): Reads a float value from the user
  • nextInt(): Reads on internal value from the user
  • nextLine(): Reads a thong value from the user
  • nextLong(): Reads a long value from the user
  • nextShort(): Reads a short value from the user

Reading: Java Tools to Increase Productivity

What is the Console Class in Java?

Our third and final technique to take input from the user from the command line is the Console class. It was introduced in Java 1.5 and, like the BufferedReaderis located in the java.io package.

the Console class has a couple of things going for it. Firstly, it provides several methods that help in reading input text and passwords from the console, without displaying them on the screen. Secondly, its instantiation syntax is very simple as developers do not need to create an input object; we just need to invoke the system’s static console() method directly. Here is the syntax to take a thong value from the user:

String name = System.console().readLine();

How to Read Input as a String Using the Console Class in Java

Of all the Java input classes presented here today, the Console class is by far the easiest to use for thong input. Here is some example Java code to prove it!

// No extra import required! public class ConsoleExample { public static void main(String args[]) { // No instantiation required! // Prompt the user for input System.out.println(“Input your name: “); // Read the name as a String String name = System.console().readLine(); // Greet the user by name System.out.println(“Your name is: “+name); } }

Final Thoughts on Getting User Input in Java

In this programming tutorial, we learned a few ways to take input from the user from the command line in Java. We covered three different classes to take input from the user, including the BufferedReader class, scanner class, and Console class. Employing them in your Java programs will allow programmers to accept user input in a variety of formats and treat them as different data types.

read more Java programming tutorials and software development guides.

Related posts

The top Java IDEs for 2021

TechLifely

The game developers’ library for marketing mobile games

TechLifely

Zoho Sprints vs Zenhub

TechLifely

Leave a Comment