1,我的意思是读取一个字符,然后判断是不是y,如果是y的话,就进入getage函数
import java.io.*;
public class student {
public static void main(String args[]){
stu s=new stu();
System.out.println("想修改年龄?y/n");
char c=' ';
try{
c=(char)System.in.read();
if(c=='y') {s.getage();System.out.println("好了");System.exit(1);}
else {System.out.println("已退出");System.exit(1);}
}catch(IOException e){}
}
}
class stu
{
void  getage()
{
int age;
String s="";
try{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
s=in.readLine();
age=Integer.parseInt(s);
}catch(IOException e){}
}
}错误提示是:
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at stu.getage(student.java:23)
at student.main(student.java:9)2,如果将上面代码的char c改成String c,然后在try里面用BufferReader进来一串字符,假设是"abc",如果用if(c=="abc") s.getage();判断的话,将什么都不发生,为什么?

解决方案 »

  1.   

    age=Integer.parseInt(s);
    ================================
    你的s这个时候仍然是"",所以转化出错。if(c=="abc")
    ===================================
    c=="abc"是一个错误,应该是"abc".equals(c).如果你要是比较内容的话.
      

  2.   

    age=Integer.parseInt(s);
    ================================
    你的s这个时候仍然是"",所以转化出错
    ========
    当我输入y的时候,就调用s.getage()啊~~~,不好意思,本人智商不高,能再详细一些好吗?
      

  3.   

    System.in.read();这种读入有问题,读的是asc码。
    判断录入也改成
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    ss=br.readLine();
    if(ss.equals("y"))
    就没有问题了