How to take a string with blank spaces as input in Java:
In this post, we will learn how to take a string with blank spaces as input in Java. We will use the next()
and nextLine()
methods to read the strings with blank spaces.
next()
and nextLine()
methods of the Scanner
class:
The next()
and nextLine()
methods of the Scanner
class can be used to read user inputs from the console. We can use any of these methods to read the user input.
But there is a difference between the next()
and nextLine()
method. The next()
method can read strings up to the space. For example, if you use the next()
method to read the string Hello World, it will read only the Hello word. It will stop if it finds any blank space.
But, we can use the nextLine()
method to read a string with blank spaces. It will read the whole Hello World string. The nextLine()
method stops if it reads a newline character \n
or if the user press the enter key.
So, if you want to take a string with blank spaces as input from the console, you have to use the newLine()
method.
Example of newLine:
Let’s take a look at the below example:
import java.util.Scanner;
public class Example1 {
public static void main(String[] args) {
try (Scanner sc = new Scanner(System.in)) {
String str;
System.out.println("Enter a string: ");
str = sc.nextLine();
System.out.println("You have entered: " + str);
}
}
}
Download it on Github
In this program,
- We have created one
Scanner
objectsc
to read the user input. - It asks the user to enter a string.
- It uses the
nextLine()
method to read the user input string and assigns it to thestr
variable. - The last line is printing the
str
variable.
If you run this program, it will print output as below:
Enter a string:
Hello World
You have entered: Hello World
Example with next():
Let’s try with the next()
method. If you use this method, it will not read the blank spaces.
import java.util.Scanner;
public class Example2 {
public static void main(String[] args) {
try (Scanner sc = new Scanner(System.in)) {
String str;
System.out.println("Enter a string: ");
str = sc.next();
System.out.println("You have entered: " + str);
}
}
}
Download it on Github
If you run this program, it will print:
Enter a string:
Hello World
You have entered: Hello
How to use the next()
method to read a string with blank spaces:
We can also use the next()
method to read a string with blank spaces. We can set the scanner’s delimiting pattern with the useDelimiter
method. It takes the pattern as the parameter. If we pass "\n"
as the parameter, it will read the string up to the new line.
For example:
import java.util.Scanner;
public class Example3 {
public static void main(String[] args) {
try (Scanner sc = new Scanner(System.in).useDelimiter("\n")) {
String str;
System.out.println("Enter a string: ");
str = sc.next();
System.out.println("You have entered: " + str);
}
}
}
Download it on Github
If you run this program, it will print output similar to the example of nextLine()
:
Enter a string:
Hello World
You have entered: Hello World
You might also like:
- Java program to check if a number is a happy number or not
- Java program to print the Neon numbers from 0 to 10000
- How to remove the first character of a string in Java
- How to remove the last character of a string in Java
- Java program to convert string characters lowercase to uppercase without using any library function
- How to convert miles to kilometers and kilometers to miles in Java
- Java program to check if a number is valid IMEI or not