0

Apart from regular expressions, parsing Strings as ints and try/catch blocks are there any other ways of handling InputMismatchException & NoSuchElementException with a Scanner?

import java.util.Scanner;

public class TestingScanner
{
    public TestingScanner() {
        tokenizeLine();
    }

    public void tokenizeLine() {    
        Scanner scanner = new Scanner("Paul 56 3125 Actor");

        String name = scanner.next();
        int age = scanner.nextInt();
        int money = scanner.nextInt();
        String occupation = scanner.next();

        System.out.println(name);
        System.out.println(age);
        System.out.println(money);
        System.out.println(occupation);

        scanner.close();
    }
}
Lewis Briffa
  • 557
  • 3
  • 6
  • 15
  • 2
    Using `hasNext()`, `hasNextInt()` etc. – RealSkeptic Mar 19 '15 at 16:47
  • 1
    as @RealSkeptic says, using 'hasNext()', and 'hasNextInt()' is a good approach. You need to make sure to read-in the null-terminator that each of these methods skips over though, otherwise you'll get input errors. – Evan Bechtol Mar 19 '15 at 17:27

0 Answers0