Now that we learned how Scanner scans inputs, we now proceed how to parse them.
Since we know that next() and nextLine() methods return strings, what if we want to scan a token that’s purely numbers, and we want to use its numeric value in a mathematical expression?
Worry not! Since we can instead parse the next token into a specific data type instead using variants of next() like nextInt(), nextFloat() or even nextBoolean()!
What is Parsing?
Depending on the context, to parse could mean to analyze something (like a text or a piece of information), making it more understandable.
In Scanner context, parsing means to convert a token (that is often expressed as a String) into a specific data type, that we can directly use in our code.
In short, it allows us to input tokens like numbers and boolean values, then use these values directly into mathematical or logical operations.
For example, consider the input stream:
123 3.14 abc
When we look at the first token in the stream, 123, we know that it can be processed into an integer value.
Subsequently, we can also process the next token in the stream 3.14 as a floating-point value.
Since the next() method only returns strings, we can utilize other variants of next() that automatically convert tokens and returns it into a specific data type we need.
This makes it possible so that we are not limited to inputting solely strings, but also input all other data types as well.
In a moment, we’ll explore each one of these functions.
Whole Numbers
To parse whole numbers, we have an array of methods that we can use:
| Method | Return Type |
|---|---|
nextByte() | byte |
nextShort() | short |
nextInt() | int |
nextLong() | long |
Like next(), if there are no tokens available to scan, it prompts a user input. Otherwise, it scans the next token in the input stream.
How can we use them in a practical way? Take a look at this example:
public class Main {
public static void main (String[] args) {
Scanner scan = new Scanner(System.in);
int num1, num2, sum;
System.out.print("Please enter the first number: ");
num1 = scan.nextInt();
System.out.print("Please enter the second number: ");
num2 = scan.nextInt();
sum = num1 + num2;
System.out.print("\nThe sum of " + num1 + " and " + num2 + " is " + sum);
scan.close();
}
}So if we have the input:
Please enter the first number: 10
Please enter the second number: 15
Then the output will be:
The sum of 10 and 15 is 25
However, due to how next() processes tokens, as described in The Input Stream, the same also applies for its variants:
Therefore, if we have the input:
Please enter the first number: 10 15
The second instance of nextInt() in line 10 would not request for user input, but instead scan the next token, 15, automatically.
So we have the resulting output:
Please enter the second number:
The sum of 10 and 15 is 25
While we have 3 more methods that we can use to parse whole numbers aside from nextInt(), they work the same way
Floating Point Numbers
In the same way as parsing whole numbers, we use two methods to parse floating point numbers:
| Method | Return Type |
|---|---|
nextFloat() | float |
nextDouble() | double |
| It also works the same way as described in [[#whole-numbers | Whole Numbers]]. |
For instance, consider this block of code:
public class Main {
public static void main (String[] args) {
Scanner scan = new Scanner(System.in);
float num1, num2, sum;
System.out.print("Please enter the first number: ");
num1 = scan.nextFloat();
System.out.print("Please enter the second number: ");
num2 = scan.nextFloat();
sum = num1 + num2;
System.out.print("\nThe sum of " + num1 + " and " + num2 + " is " + sum);
scan.close();
}
}So if you have the input:
Please enter the first number: 3.2
Please enter the second number: 1.9;
Then the output is:
The sum of 3.2 and 1.9 is 5.1
The same way on how next() handles tokens, the same goes for nextFloat() and its variants.
So if you have the input:
Please enter the first number: 3.2 1.9
The next instance of nextFloat() will not await for user input (as there are still remaining tokens to scan), and instead scan the next token in the stream 1.9
Therefore, the output is:
Please enter the second number:
The sum of 3.2 and 1.9 is 5.1
One major quirk for using nextFloat() and its variants is if you enter a whole number without a floating point part, like 3 or even 69.
Please enter the first number: 3
Please enter the second number: 69
It automatically gets converted to a floating point value.
Therefore, the output is:
The sum of 3.0 and 69.0 is 72.0
Boolean Types
One other data types that the Scanner can detect are boolean values.
If your input contains the string true or false, then the scanner can parse that as a boolean data type.
Consider this block of code:
public class Main {
public static void main (String[] args) {
Scanner scan = new Scanner(System.in);
boolean bool;
System.out.print("Are you Left-Handed? (True/False): ");
bool = scan.nextBoolean();
if (bool) System.out.printf("EVEN BETTER!!!");
else System.out.print("Approved :>");
scan.close();
}
}If the input is:
Are you Left-Handed? (True/False): true
Then the output is:
EVEN BETTER!!!
When scanning for boolean values with nextBoolean(), it is not case-sensitive, therefore, it can detect for boolean values even if our input is:
Are you Left-Handed? (True/False): fAlSe
It will still be parsed as false.
Approved :>