This article demonstrates the procedure to take an input string with blank spaces in Java and in this regard, this article covers the following concepts:
- How to Take an Input String With Blank Spaces in Java?
- Using the “nextLine()” Method
- Using the “next()” Method
How to Take an Input String With Blank Spaces in Java?
Taking an input string with blank spaces provides more flexibility in user input; they can input phrases, sentences, or multi-word values without the need for special formatting. It helps in text processing and analysis, file and path handling which is important in applications that involve file manipulation, or file input/output. The input string with blank space can be taken by utilizing the “next()” and “nextLine()” methods.
Method 1: Using the “nextLine()” Method
The “nextLine()” method is utilized for taking blank spaces in Java for retaining spaces. It can also be utilized to read multiple words, handle mixed input, provides a user-friendly experience, and offers versatile input-handling capabilities.
Visit the below code block that takes input from the user that contains blank spaces and then returns the same string:
class linuxhint{
public static void main(String[] args) {
try (Scanner blankInput = new Scanner(System.in)) {
String string;
System.out.println("Enter a string that contains spaces: ");
string = blankInput.nextLine();
System.out.println("You have entered this string: " + string);
}
}
}
In the above code block:
- First, the library name “Scanner” is imported using the “import java.util.Scanner;” command.
- Next, the “Scanner” object named “blankInput” is initialized to read input from the standard input stream “System.in” in Java.
- Then, the “string” variable has been created having a type set to “string”.
- After that, the “nextLine()” method that is accessible using the object of the “Scanner” library is assigned to a newly received string from the end user.
- In the end, the updated “string” is printed on the console.
After the end of the compilation phase:
The above GIF shows the input string having blank space that has been stored and then retrieved in Java.
Method 2: Using the “next()” Method
The “next()” method takes inputs having black spaces because it only reads the next token until a space or whitespace is encountered. It fails to store the whole line of input which makes it appropriate for reading individual tokens or words separated by spaces or other whitespace characters.
Visit the below code for practical implementation:
public static void main(String[] args) {
try (Scanner blankInput = new Scanner(System.in)) {
String string;
System.out.println("Enter a string that contains spaces: ");
string = blankInput.next();
System.out.println("You have entered this string: " + string);
}
}
}
The above code is the same as the above method. Only, the “next()” method is attached with the string coming from the user.
After the end of the compilation phase, the output appears like this:
The output confirms that only the first part of the string that is before the white space is displayed on the console.
However, the above issue can be resolved to accept the whole string containing whitespaces. To do this, the “useDelimiter” method is used while the creation of the “Scanner” library object. To set the delimiting pattern of the Scanner that is in use.
For instance, the “\n” is provided as a parameter to this “useDelimiter” method as shown below code:
public static void main(String[] args) {
try (Scanner blankInput = new Scanner(System.in).useDelimiter("\n")) {
String string;
System.out.println("Enter a string that contains spaces: ");
string = blankInput.next();
System.out.println("You have entered this string: " + string);
}
}
}
After the end of the compilation phase:
The output shows that a string having blank space is also being accepted using the “next()” method.
Conclusion
To take input strings with blank spaces, the “Scanner” library “nextLine()” and “next()” methods are utilized in Java. The “nextLine()” method is assigned with the string that the developer wants to accept white characters. On the other hand, the “next()” does not accept the blank spaces as it accepts strings till it encounters any whitespace. To make it work, the scanner “useDelimiter()” method is used having “\n” as a parameter.