Parameters of Java Scanner next()
The next() method takes no parameters. It simply reads the next token from the input source without requiring any additional information.
However, it's important to note that the next() method has some default behavior when it comes to delimiting tokens. By default, it considers whitespace characters (spaces, tabs, line breaks) as delimiters between tokens. This means that it will read the input until it encounters a whitespace character & return the characters before that as a single token.
For example, if the input is "Hello World", calling next() twice will return "Hello" and then "World" as separate tokens:
Scanner scanner = new Scanner(System.in);
String token1 = scanner.next(); // reads "Hello"
String token2 = scanner.next(); // reads "World"
Note: If you want to use a different delimiter for separating tokens, you can use the useDelimiter() method of the Scanner class to specify a custom delimiter pattern before calling next().
Return Value of Java Scanner next()
The next() method returns the next token from the input as a string. It reads the characters from the input source until it encounters a delimiter (by default, a whitespace character) and returns all the characters before the delimiter as a single string.
If the input has no more tokens available, the next() method will throw a NoSuchElementException. This exception indicates that there are no more elements (tokens) to read from the input source.
For example :
Scanner scanner = new Scanner("Hello World");
String token1 = scanner.next(); // returns "Hello"
String token2 = scanner.next(); // returns "World"
In this example, we create a Scanner object with the string "Hello World" as the input source. When we call next() the first time, it reads the characters until it encounters the space delimiter and returns "Hello" as a string. The second call to next() reads the remaining characters after the space and returns "World" as a string.
Note: It's important to remember the case when there are no more tokens available in the input to avoid the NoSuchElementException. You can use the hasNext() method to check if there are more tokens before calling next().
Exceptions of Java Scanner next()
The next() method can throw two types of exceptions:
1. NoSuchElementException: This exception is thrown if there are no more tokens available in the input source when next() is called. It indicates that the scanner has reached the end of the input & there are no more elements to read.
Example:
Scanner scanner = new Scanner("Hello");
String token1 = scanner.next(); // returns "Hello"
String token2 = scanner.next(); // throws NoSuchElementException
In this example, the second call to next() will throw a NoSuchElementException because there are no more tokens available after reading "Hello".
2. IllegalStateException: This exception is thrown if the scanner is closed & next() is called. Once a scanner is closed, it cannot be used to read input anymore.
Example:
Scanner scanner = new Scanner("Hello");
scanner.close();
String token = scanner.next(); // throws IllegalStateException
In this example, we close the scanner using the close() method & then try to call next(). This will throw an IllegalStateException because the scanner is already closed.
To avoid these exceptions, you should:
- Always check if there are more tokens available using the hasNext() method before calling next().
- Close the scanner only when you are done reading the input & don't try to use it after closing.
Let’s see an example that shows how to safely use next() without throwing exceptions:
Scanner scanner = new Scanner("Hello World");
while (scanner.hasNext()) {
String token = scanner.next();
System.out.println(token);
}
scanner.close();
In this example, we use a while loop to check if more tokens are available using hasNext(). We call next() only if hasNext() returns true. After reading all the tokens, we close the scanner.
Example
Let's look at a complete example that shows how to use Java Scanner next() method:
import java.util.Scanner;
public class ScannerNextExample {
public static void main(String[] args) {
// Create a scanner to read input from the keyboard
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your full name: ");
// Read the first token (first name)
String firstName = scanner.next();
// Read the second token (last name)
String lastName = scanner.next();
System.out.println("First Name: " + firstName);
System.out.println("Last Name: " + lastName);
// Close the scanner
scanner.close();
}
}

You can also try this code with Online Java Compiler
Run Code
In this example, we create a Scanner object to read input from the keyboard using `Scanner(System.in)`. We prompt the user to enter their full name using `System.out.print()`.
We then use `scanner.next()` to read the first token (first name) from the user input and store it in the `firstName` variable. Similarly, we use `scanner.next()` again to read the second token (last name) and store it in the `lastName` variable.
Finally, we print the first and last names using `System.out.println()` and close the scanner using `scanner.close()`.
Output:
Enter your full name: Harsh Singh
First Name: Harsh
Last Name: Singh
As you can see, the program prompts the user to enter their full name. When the user enters "Harsh Singh," the first call to `next()` reads "Harsh" as the first token, and the second call to `next()` reads "Singh" as the second token. The program then prints the first name and last name separately.
This example shows how to use the Scanner next() method to read multiple tokens from user input and process them accordingly.
What is Java Scanner next()?
The Java Scanner next() method is part of the Scanner class in the Java.util package. It reads the next token (word) from the input source, which can be the keyboard, a file, or a string.
When you call the next() method, it reads the characters from the input source until it encounters a delimiter (by default, a whitespace character such as a space, tab, or new line). It then returns all the characters read before the delimiter as a single string (token).
The next() method is commonly used to read individual words or tokens from user input or from a file. It provides an easy way to parse the input into separate parts based on the delimiters.
Let’s see a few important points about the Java Scanner next() method:
It reads the next token from the input source & returns it as a string.
It uses whitespace characters (space, tab, new line) as the default delimiters to separate tokens.
It throws a NoSuchElementException if there are no more tokens available in the input.
It throws an IllegalStateException if the scanner is closed & next() is called.
- It can be used in combination with other Scanner methods like hasNext() to check if there are more tokens available before calling next().
The Scanner next() method is particularly useful when you need to read individual words or values from user input or from a file. It provides a convenient way to parse the input into separate tokens and process them according to your program's requirements.
For example, you can use next() to read a series of numbers entered by the user:
Scanner scanner = new Scanner(System.in);
System.out.print("Enter three numbers: ");
int num1 = scanner.nextInt();
int num2 = scanner.nextInt();
int num3 = scanner.nextInt();
scanner.close();
In this example, next() is used to read three separate integers from the user input.
The Scanner next() method is a main tool for reading input in Java and is widely used in many situations where parsing input into tokens is required.
More Examples
Let’s look at a few more examples that shows the use of the Java Scanner next() method in different situations:
Example 1: Reading multiple strings from user input
import java.util.Scanner;
public class WordInput {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter three words: ");
String word1 = scanner.next();
String word2 = scanner.next();
String word3 = scanner.next();
System.out.println("Words entered: " + word1 + ", " + word2 + ", " + word3);
scanner.close();
}
}

You can also try this code with Online Java Compiler
Run Code
Output:
Enter three words: Hello Java World
Words entered: Hello, Java, World
Example 2: Reading tokens from a string
import java.util.Scanner;
public class TokenScanner {
public static void main(String[] args) {
String input = "Welcome to Java Programming";
Scanner scanner = new Scanner(input);
while (scanner.hasNext()) {
String token = scanner.next();
System.out.println(token);
}
scanner.close();
}
}

You can also try this code with Online Java Compiler
Run Code
Output
Welcome
to
Java
Programming
Example 3: Using a custom delimiter
import java.util.Scanner;
public class FruitScanner {
public static void main(String[] args) {
String input = "apple,banana,cherry";
Scanner scanner = new Scanner(input).useDelimiter(",");
while (scanner.hasNext()) {
String fruit = scanner.next();
System.out.println(fruit);
}
scanner.close();
}
}

You can also try this code with Online Java Compiler
Run Code
Output
apple
banana
cherry
In this example, we use the useDelimiter(",") method to specify a custom delimiter (comma) instead of the default whitespace delimiter.
Note: Always check if there are more tokens available using hasNext() before calling next() to avoid exceptions. Also, remember to close the scanner when you're done using it to free up system resources.
Frequently Asked Questions
What happens if there are no more tokens available when calling the Scanner next() method?
If there are no more tokens available in the input source, the Scanner next() method will throw a NoSuchElementException.
Can I use a different delimiter other than whitespace with the Scanner next() method?
Yes, you can use the useDelimiter() method of the Scanner class to specify a custom delimiter pattern before calling next().
Is it necessary to close the Scanner after using the next() method?
Yes, it is recommended to close the Scanner using the close() method when you are done using it to free up system resources.
Conclusion
In this article, we discussed the Java Scanner next() method, which reads the next token from an input source. We learned about its syntax, parameters, return value, and exceptions. We also saw different examples of situations like how to use next() to read tokens from user input, strings, and with custom delimiters. The Scanner next() method is a versatile tool for parsing input into individual tokens in Java programs.
You can also check out our other blogs on Code360.