Integer.parseInt("1234");,注意,如果包含有非數字字符,會丟出異常,捕捉一下就行了

解决方案 »

  1.   

    try{byte buffer[]=new byte[30];
    System.in.read(buffer);
    String strBuffer=new String(buffer);
    System.out.println(strBuffer);
    }
     catch(Exception ex)
        {
          ex.printStackTrace();
        }
      

  2.   

    int i = Integer.parseInt(strBuffer);
      

  3.   

    new BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
    while(true){
    String s=in.readLine();
    try{int tt=Integer.parseInt(s);}catch(Exception e){e.printStace();}
    }
      

  4.   

    class InOutDemo
    {
    public static void main(String args[])throws Exception
    {
    byte buffer[]=new byte[30];
    System.in.read(buffer);
    String strBuffer=new String(buffer);
    System.out.println(strBuffer);
    int i=Integer.parseInt(strBuffer);
    System.out.println(strBuffer);
    }
    }
    谢谢大家,现在改成这样了,可我明明输入的是123,可为什么还是会出现异常呢?
      

  5.   

    在你的程序中使用了SYSTEM.IN,从中读取输入信息,但是你应该捕获IOException异常,把System.in.read(buffer);改为
    try {
    System.in.read(buffer);
    } catch(IOException e) {
    //处理异常的操作 
    }
      

  6.   

    你限制了
    byte buffer[] = new byte[30]
    输入了123,
    但内容却是123__________...'_'是空格
    试试int i = Integer.parseInt(strBuffer.trim());
      

  7.   

    另外你还要捕获一个异常,就是你使用Integer.parseInt(strBuffer);的地方
      

  8.   

    异常呀,哦,这就把书上有关异常的那章看看.
    to Kick_hotdog(khd)
    你那样说应该是针对buffer这个字节数组可我已经把它转换成strBuffer这个字符串了应该不存在空的内容才对吧,不知道我这样理解对不对.
      

  9.   

    正在看有关异常的那章内容好多,那位大侠能写段完整的程序吗?要求很简单在程序里定义一个INT I,然后通过键盘输入给I附值.