避免InputMismatchException的方法& NoSuchElementException异常?

时间:2015-03-19 16:44:05

标签: java java.util.scanner nosuchelementexception inputmismatchexception

除了正则表达式,将字符串解析为int和try / catch块还有其他任何方法可以处理InputMismatchException&带扫描仪的NoSuchElementException?

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();
    }
}

0 个答案:

没有答案