java读取整数遇到的问题

来源:百度知道 编辑:UC知道 时间:2024/09/24 01:16:50
byte[] b = new byte[20];
System.in.read(b);
String s = new String(b);
int a = Integer.parseInt(s);
这个为什么不行啊?提示最后一句跑出NumberFormatException

楼上胡扯吧
NumberFormatException异常是说 Integer.parseInt(s); 时,s有非数字字符的异常(比如: s是as)

把byte数组转成string 不行吧?

你输入的可能不是数字,在最后一句加上 try……catch……

String s = new String(b);

你这样吧字节数组转换成字符串,会出乱码的, 你还是好好看一看java api吧