For example if I have a text file showing different integers, but if it comes across a value where it has a letter for example, it would throw a NumberFormatException. I have seen many times where a try-catch statement would be used, but is there any other way to handle this exception besides that? Here is an example for a txt file called "data" (Note that there are three integers separated by whitespace)
545F6 6 100
12N45 A 50
would the following code work?
while (data.hasNextLine()){
data.nextInt();
if (!data.hasNextInt()){
System.out.println("The number " + data.next() + " is invalid");
data.next();
}
}
I am beginner in Java so I was curious if there was another way to ignore the strings, and show that it is invalid if it does not return an integer.