import java.io.*;public class FaceTest1 {
public static void main(String args[]) {

String str = "";

BufferedInputStream bis = new BufferedInputStream(System.in);
try {
System.out.println("请输入字符串:");
byte []b = new byte[100];
int scount = bis.read(b);
str = new String (b,0,scount);
                           System.out.println(str);
            System.out.println("请输入要取的字符个数:");
            byte c[] = new byte[10];
    int count = bis.read(c);
    String cn =new String(c,0,count);
    int m = Integer.parseInt(cn); //这一行报错,类型转换错误,请问为什么呢?
     System.out.println(m);
InputStreamReader is = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(is);
/* try {
System.out.println("请输入字符串:");
str = br.readLine();
System.out.println("请输入要取的字符个数:");
n = Integer.parseInt(br.readLine());*/
   
}catch (IOException e){
e.printStackTrace();
}catch(NumberFormatException e){
System.out.println("numerror");
e.printStackTrace();
}

}

解决方案 »

  1.   

    int m = Integer.parseInt(cn); //这一行报错,类型转换错误,请问为什么呢?
    当你的cn不能转换为int的时候报错.也就是说你的这个字符串中除了数字外还有其他字符
      

  2.   

    楼主,帮你看了一下
    java.lang.NumberFormatException: For input string: "1
    "
    请看上面这个错误,这个是因为回车键的原因
      

  3.   

    帮你截取了字符串,使用了转义字符
    String cn = new String(c, 0, count);
    String[] s = cn.split("\r");
    int m = Integer.parseInt(s[0]); // 这一行报错,类型转换错误,请问为什么呢?
    System.out.println(m);