How to Take String Input in Java?
Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java
While taking input from users to create an interactive program, more often than ever the user will give input in the form of Strings. Strings represent the way we humans interact with each other, thus it is very important for the programmer to code their program in such a way that the user is able to express himself in the form of Strings. Let us take a look at How to Take String Input in Java?
Ways to take string input in Java:
Java provides various classes and methods to facilitate String input. They are as follows:
- Using BufferedReader class readLine() method.
- Using Scanner class nextLine() method.
- Through Scanner class next() method.
- Using Command-line arguments of the main() method.
Let us discuss each of these methods individually.
1. Using Java BufferedReader class readLine() method:
The BufferedReader class is the most native way to take input from users. It is part of the java.io package. The BufferedReader class contains a method called readLine() which facilitates user input in the form of Strings.
The signature of the readLine() method is given by:
public String readLine() throws IOException
The readLine() method returns the string from the input buffer after removing the terminating character.
At first, we need to call the BufferedReader class object, which is wrapped around the System input, then using that object we need to call the readLine method, which will facilitate the string user input.
The Syntax is as follows:
BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); String str= br.ReadLine();
Code to understand BufferedReader Input:
package com.DataFlair.StringInput; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class BufferedReaderInput { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter anything: "); String str = reader.readLine(); System.out.println("You have Entered: "); System.out.println(str); } }
The output of the above code:
Enter a StringDataFlair String Input Tutorial
The String input using Scanner class is: DataFlair String Input Tutorial
There is another implementation of the BufferedReader class, which allows the user to give input until a specified character is entered.
Code to take Input using BufferedReader until the User enters any specified character:
package com.DataFlair.StringInput; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class BufferedReaderInput2 { public static void main(String[ ] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = ""; System.out.println("Enter the Strings and when you want to stop entering the Strings, type ‘.’"); while(!str.equals(".")) { System.out.println("Enter a String: "); str = br.readLine(); System.out.println("The String input is: "+str); if(str.contentEquals(".")) System.out.println("FullStop!!!"); } br.close(); } }
The output of the above code is:
Enter the Strings and when you want to stop entering the Strings, type ‘.’Enter a String:Â
DataFlair
The String input is: DataFlair
Enter a String:Â
Java Tutorial
The String input is: Java Tutorial
Enter a String:Â
Console Input
The String input is: Console Input
Enter a String:Â
.
The String input is: .
FullStop!!!
2. Using Scanner class nextLine() method:
The Scanner class also known as the utility scanner is the more widely used class for taking user input. It is part of the java.util package. The Scanner class contains the nextLine() method which allows user input in the form of string.
The signature of the nextLine() method is given by:
public String nextLine()
The method returns the entered string from the input buffer. If the method is unable to find any string input, it throws the NoSuchElementException. If we close the scanner object and then try to take input, it throws the IllegalStateException.
First, we need to create a Scanner class object which wraps around the system input. After that using the object we call the nextLine() method to take String input.
The Syntax is given as:
Scanner sc = new Scanner(System.in); String str = sc.nextLine();
Code to understand Java Scanner Class Input using nextLine():
package com.DataFlair.StringInput; import java.util.*; public class ScannerClassInput { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter a String"); String str = sc.nextLine(); System.out.println("The String input using Scanner class is: " +str); } }
The output of the above code:
Enter a StringDataFlair String Input Tutorial
The String input using Scanner class is: DataFlair String Input Tutorial
3. Using the Scanner Class next() method:
The Scanner class also has another method that facilitates string input. The next() method lets the programmer take user input in the form of a string, but only accepts the string until the first space is encountered. This method can be useful when the programmer needs to ensure that the user does not enter more than one word as input.
The signature of the next() method is given by:
public String next()
The next method basically returns the next token that it encounters, i.e., until an empty character is encountered. If there is no next token it throws the NoSuchElementException. If we close the Scanner class object and then call the method, it throws the IllegalStateException.
Code to understand Scanner Class Input using next():
package com.DataFlair.StringInput; import java.util.*; public class ScannerClassInput { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter a String"); String str = sc.next(); System.out.println("The String input using Scanner class is: " +str); } }
The output of the above code:
Enter a StringDataFlair String Input Tutorial
The String input using Scanner class is: DataFlair
We can see that only the first word is accepted by the next() method, this is useful in various scenarios where only one word needs to be taken as input.
4. Using Command-line arguments of the main() method:
We can take string input using command line arguments, i.e, through the main() method’s arguments. They are generally represented with the args[] variable.
Code to understand String input using command-line arguments:
package com.DataFlair.StringInput; public class CommandLineInput { public static void main(String args[]) { int i = 0; for (i = 0; i < args.length; i++) { String str = args[i]; System.out.print(str+" "); } } }
The output of the above code:
Conclusion:
So, we can see that there are various techniques to take string input in Java. In this article, we saw how to take user input using, readLine() method of the BufferedReader class, nextLine() method of the Scanner class, Next() method of the Scanner class and using command line arguments. They are all very important methods and can be used in various ways to take input from the user.
Did we exceed your expectations?
If Yes, share your valuable feedback on Google