Introduction
If we read an Integer first and then read strings, it is necessary to add another nextLine() between the nextInt() and nextLine() methods. Otherwise, there will be an InputMismatchException.
1. Program with Exceptions
Codes
1 | import java.io.File; |
The content of the test.txt file
1 | 100 |
Outputs
1 | 100 |
2. Program without Exceptions
Codes
1 | import java.io.File; |
The content of the test.txt file
1 | 100 |
Outputs
1 | 100 |
Word count: 222
References
1.Oracle. (2020). Class Scanner (Java Platform SE 7).
https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html