How to take String Input in Java?

There are various ways to take String input in Java. In this section, we are going to discuss how to take String input in Java. There are following ways to take String input in Java:

1) By Using Java Scanner class

2) By Using Java BufferedReader class

3) By Using the Command Line argument

By Using Java Scanner class

The Scanner class is defined in java.util package that is used to take input from the user. The Scanner class provides the following two methods to take input from the user.

  1. Scanner.nextLine() Method
  2. Scanner.next() Method

Let’s discuss each of the mentioned methods to take input from the user.

  1. Scanner.nextLine() Method

The nextLine() method reads the input till the line gets terminated and shifts the cursor to the next line. The syntax of the nextLine() method is:

Syntax:

The method does not accept any parameter. It returns the string that was skipped. If the method finds no line to read, the method throws the NoSuchElementException.

FileName: StringInputExample.java

Output:

String Input in Java 1

Explanation: After displaying the message Enter a string, the cursor waits on the console for the user to enter string. The method nextLine() reads till the user hits enter and return the read stuff, which is captured by the variable str. The same read string is shown in the output. Note that hitting the enter button shows that the line has been terminated.

  • Scanner.next() Method

The next() method reads the input till the line gets terminated or white space is encountered. The main difference between the nextLine() and next() method is that the latter one terminates when white space is encountered, where the former one terminates only when enter is pressed. The syntax of the next() method is:

Syntax:

Returns:

The read line is returned.

The method does not accept any parameter. It returns the string that was skipped. If the method finds no line to read, the method throws the NoSuchElementException.

FileName: StringInputExample1.java

Output:

String Input in Java 1

Explanation: The user enters My Name is Khan. However, after the word “My” white space is encountered. Hence, the rest three words are not read, and the same can be confirmed by observing the output.

By using Java BufferedReader Class

Java BufferedReader Class is used to read the stream of characters. The class accepts an object of the InputStreamReader class. So, it is necessary to create a constructor of the InputStreamReader class and pass its object to the BufferedReader class as a parameter. The BufferedReader class has the readLine() method to take input from a user. The readLine() method reads one line at a time.

Syntax:

It returns the line entered by the user. It does not include any line-termination characters, or null if the end of the stream has been reached.

Let’s observe the following program to understand how the BufferedReader class is used for taking input from the user.

FileName: StringInputExample2.java

Output:

String Input in Java 1

Explanation: In the constructor of the InputStreamReader class, System.in is passed. It is done because the input is being taken from the keyboard. With the help of InputStreamReader class, the BufferedReader class stores the input given by the user through keyboard.

By using the Command Line Argument

Command Line argument is present in the main method (String argvs[]). String argvs[] is a string array that accepts a line as an input. Let’s understand the usage of command-line argument with the help of the following Java program.

FileName: StringInputExample3.java

Output:

String Input in Java 1

Explanation: The command line argument is put when the program is executed using the java command. The number of elements in the String array argvs[] is decided by the white spaces present in the input string. In the input string (“My name is Khan”), there are three white spaces present. Hence, there are four string elements present in the array argvs (“My”, “name”, “is”, “Khan”). The array argvs is then parsed using the for-loop, and elements of the array argvs are then concatenated to build the string again, as we have shown in the above output.

Pin It on Pinterest

Share This