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.   

    import java.io.*;
    public class A {
            
        /**
         * Creates a new instance of <code>A</code>.
         */
        public A() {
        }
        
        /**
         * @param args the command line arguments
         */
        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("ok");System.exit(1);}
         else {System.out.println("quit");System.exit(1);}
       }catch(IOException e){}
    }
    }class stu
    {
         void  getage()
        {
        int age;
        String s="";
        try{
         System.out.println("ready!");
        
        BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
        in.skip(2);
        s=in.readLine();
        age=Integer.parseInt(s);
        System.out.println(age);
        }catch(IOException e){e.printStackTrace();}
        }
    }
      

  2.   

    1. 这是在读取字输出的字符串后再敲了个回车的缘故,第一个字符传给了c,而那个回车符则传给了s,s就变成回车符了,不能转成数字类型的,就导致出错了,建议把
         c=(char)System.in.read();
      改成:
         Scanner sca = new Scanner(System.in);
     c = sca.nextLine().charAt(0);
      应该就没有什么问题了。2. String是一个对象不是基本类型不能采用“==”来比较的,比较相等应该采用 c.equals("abc"); 或者是 c.equalsIgnoreCase("abc") 来比较的。在对象中“==”只能比较这两个对象是否相同(或者说是它们的内存地址是否相同),并不是比较其所指向的内容。
      

  3.   

    当你c=(char)System.in.read();输出缓存里缓存了/r/n(windows 平台下),c 为字符时,只是取一个字符,此时游标指向了/r,所以当你BufferedReader in=new BufferedReader(new InputStreamReader(System.in));会出现异常,所以in.skip(2);跳过两个字符,使游标指向有效数字串
      

  4.   

    ===================
    to:bao110908(Baobao)
    谢谢你的回答,我用了一下Scanner,出错了,主要还没接触Scanner,以后会好一些的吧,我觉得grant999(民)在三楼的解答不错
    关于你的第二个解答,我试了一下,很好用,呵呵,谢谢了
    ===================
    to:grant999(民)
    恩,谢谢啦,懂了
    p.s. in.skip(???)这个数字有几种方式,比如有in.skip(3) or 其他吗?
      

  5.   

    参数是long类型就行,不过要注意是跳过的是字节还是字符
      

  6.   

    to: lz
    Scanner是一个很好用的类,是JDK5.0中新增的类,里面有好多 nextXXX()的方法,直接读取不同格式的字符很方便的,不用再进行转换了。