String Input in Java
Overview
With the help of the I/O package, Java has several Streams that help the user in performing all the input-output activities. These streams support all types of objects, data types, characters, and so on.
For taking string input we can make use of various methods like using Scanner class, BufferedReader class, and Command Line argument.
How to Take String Input in Java ?
Input and Output are one of the most important parts of learning any programming language. Java has a various number of I/O Streams that help the user in performing all the input-output activities, taking string input from the user can be done using these I/O streams. Further in this article, we will discuss all those approaches that are needed to be followed for taking string input.
Method - 1 : By Using Java Scanner Class
The Scanner class is provided by the java.util package and is used to take input from the user. The Scanner class has the following methods that help us to take input from the user :
- Scanner.nextLine()
- Scanner.next()
For using the Scanner class, we have to import the Scanner Class in our code by using the following syntax :
Now let's see how these methods help us to take string input in Java.
Scanner.nextLine() Method
The Scanner class's nextLine() method is used to take string input from the end user. nextLine() method is contain java.util.Scanner class. This method reads text until it reaches the end of the line. It moves the cursor to the next line after reading the line.
Syntax :
The skipped line is returned by the nextLine() method. It doesn't take any parameters. When the nextLine() method can't find a line, it throws a NoSuchElementException. If the scanner class is closed, it also throws an IllegalStateException.
Now let's take a look at an example of the nextLine() method :
Example : 1
In this example, we are importing the scanner class using the nextLine() method followed by System.in standard input stream
Output :
Example : 2
In this example, we are showing the case when this method throws NoSuchElementException.
Output :
We haven't provided any std input to our java program, so it will throw NoSuchElementException.
Scanner.next() Method
The Scanner class's next() method in Java is used to read the input before the whitespace is detected. next() method is unable to read two words separated by whitespace. After reading the input, it keeps the pointer in the same line.
Syntax :
Next complete string value is returned by next() method. It doesn't take any parameters. When the next() method can't find string value, it throws a NoSuchElementException. If the scanner class is closed, it also throws an IllegalStateException.
Now let's take a look at an example of the next() method :
Example : 1
In this example, we are importing the scanner class for using the next() method followed by System.in standard input stream
Output :
Example : 2
In this example, we are showing the case when this method throws NoSuchElementException.
Output :
We haven't provided any std input to our java program, so it will throw NoSuchElementException.
Difference Between Scanner.nextLine() and Scanner.next()
The key difference between the nextLine() and next() method is that the next() method gets terminated when whitespace is encountered, whereas the nextLine() method terminates only when enter is pressed and next line is reached.
Method - 2 : By Using Java BufferedReader Class
The BufferedReader class is provided by the java.io package and is used to read the stream of characters, BufferedReader accepts the InputStreamReader class as the parameter for its execution.
Some basic differences between Scanner class and BufferedReader class :
- BufferedReader is synchronous while the Scanner is not. So, BufferedReader should be used if we are working with multiple threads.
- BufferedReader has a significantly larger buffer memory than Scanner.
- BufferedReader is slightly faster than the scanner since the scanner parses the input data, whereas BufferedReader reads a sequence of letters.
BufferedReader class has the readLine() method for taking string input from the user. readLine() method reads a single line at a time.
Syntax :
The readline() method returns the line that the user has entered. Now let's take a look at an example of how the BufferedReader class is used for taking string input.
Example :
In this example, we are using BufferedReader with InputStreamReader for the standard IO stream, also BufferedReader must be used inside the try-catch block in case of exceptions.
Output :
Method - 3 : By Using the Command Line Arguments
In Java programs, we can pass the command line argument in the main method of the driver program.
String args[] is a string array that takes a line as an input. So, we can pass a string as an argument to the main method and can fetch its value and use it.
Example :
In this example, we have used the command line argument using the args[] array and used its value for displaying the string output.
Command Line Argument :
Output :
Conclusion
- Java has several Streams that help the user in performing all the input-output activities.
- String input can be achieved using Scanner class, BufferedReader class, and Command-line Arguments
- The Scanner class is provided by java.util package and uses the following methods for string input :
- Scanner.nextLine() :
This method reads text until it reaches the end of the line. - Scanner.next() :
This method reads the input before the whitespace is detected.
- Scanner.nextLine() :
- The BufferedReader class is provided by java.io package and uses readLine() method for string input.
- we can also pass command line argument in the main method of the driver program.