设计一个程序,其功能是从命令行输入整数字符串,再将该整数字符串转换为整数,输入的数据可能具有以下格式:12345123   45123xyz456对这种异常进行捕获和处理;谢谢!!

解决方案 »

  1.   

    你只是要捕获异常?try {
      num = Integer.valueOf(str);
    } catch (NumberFormatException ex) {
      ex.printStackTrace();
    }
      

  2.   

    伸手党啊,如果你真有心学习,就好好看书吧。Scanner sc = new Scanner(System.in);
    String line = sc.nextLine();
    int num = -1;
    try {
                num = Integer.valueOf(line);
                System.out.println("Number:" + num);
    } catch (NumberFormatException ex) {
                System.err.println("输入的不是标准整数!");
    }