注释部分为问题所在.如何能让第2次循环有效.
输入数据如下:
0
y
结果出现NumberFormatException异常.怀疑是输入的y存在于输入缓冲区中,导致进入第2次循环不需要输入数据(y应该被读取了).
执行subscription = Integer.parseInt(str)-1;语句.当然就会出现NumberFormatException异常了.
不知道我的看法对没对,请问如何解决能过输入第2次数据的问题?(就是继续输入月份.)
[code=Java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;public class MonthDemo {
private static String[] month;
private static int subscription;
public static void main(String[] args) {
month = new String[]{"January","February","March","April","May","June",
"July","August","September","October","November","December",};
boolean flag = true;
char c;
String str;
while(flag){
System.out.print("请输入月份的数字: ");
try{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
str = br.readLine(); //第2次循环到这里不需要输入数据,怀疑是输入缓冲区没有清空,输入的非n字符被读取.
subscription = Integer.parseInt(str)-1;
try{
if(subscription<0 || subscription>11)
{
throw new MyException();
}
else
{
System.out.println((subscription+1)+"月的英文为:"+month[subscription]);
flag = false;
}
}
catch(MyException e)
{
System.out.println(e);
System.out.println("退出请输入N,不退出则输入任意字符");
try{
c = (char)System.in.read();
if(c =='n' || c=='N' )
{
flag = false;
}

}
catch(IOException ex){
ex.printStackTrace();
}
}
}
catch(IOException e)
{
System.out.println("读入数据时出错~!");
}
}
}
}class MyException extends Exception{
MyException(String str){
super(str);
}
MyException(){
super();
}
public String toString(){
return "您输入的月份不合法~!";
}
}
][/code]

解决方案 »

  1.   

    不好意思代码没贴好...import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;public class MonthDemo {
    private static String[] month;
    private static int subscription;
    public static void main(String[] args) {
    month = new String[]{"January","February","March","April","May","June",
    "July","August","September","October","November","December",};
    boolean flag = true;
    char c;
    String str;
    while(flag){
    System.out.print("请输入月份的数字: ");
    try{
    InputStreamReader isr = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(isr);
    str = br.readLine(); //第2次循环到这里不需要输入数据,怀疑是输入缓冲区没有清空,输入的非n字符被读取.
    subscription = Integer.parseInt(str)-1;
    try{
    if(subscription<0 || subscription>11)
    {
    throw new MyException();
    }
    else
    {
    System.out.println((subscription+1)+"月的英文为:"+month[subscription]);
    flag = false;
    }
    }
    catch(MyException e)
    {
    System.out.println(e);
    System.out.println("退出请输入N,不退出则输入任意字符");
    try{
    c = (char)System.in.read();
    if(c =='n' || c=='N' )
    {
    flag = false;
    }

    }
    catch(IOException ex){
    ex.printStackTrace();
    }
    }
    }
    catch(IOException e)
    {
    System.out.println("读入数据时出错~!");
    }
    }
    }
    }class MyException extends Exception{
    MyException(String str){
    super(str);
    }
    MyException(){
    super();
    }
    public String toString(){
    return "您输入的月份不合法~!";
    }
    }
      

  2.   

      首先呢,你出的问题并不是没有清空缓存的原因,而是逻辑问题,最明显的就是成功输出月份的英文后你把flag=false他自然就停止循环了
      第2,你在第一个try那里捕捉的是IOException,也就是说,如果你输入数字n不能被Integer.parseInt(n),NumberFormatException这个异常你就捕获
    不到,可以改成Exception
      自己把逻辑再整理下,很简单就能改好
      

  3.   

    首先,如何成功输入后,不会进入第2次循环.因为已经退出了.所以与你说的没有一点关系.
    第2.我现在不是要捕获NumberFormatException.
    最后,还是感谢你的回帖.
      

  4.   

    楼主输入y后,执行subscription = Integer.parseInt(str)-1;后会抛出NumberFormatException异常,注意,抛出异常后,try下面的语句就不执行了,所以直接到catch语句那里,而你又没捕捉该异常,那么java运行时系统就捕捉并输出了
    另外,流中的数据不会存在于缓冲区的,readLIne()已经读出了!
      

  5.   

    我先输入的是0,然后再输入的是y...不是一开始输入y.暂时先不考虑一开始就输入y的情形,不知道我说清楚没?
      

  6.   

    这是我修改后的代码,也不是很好,但能实现楼主效果,你可以再改一改!
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;public class MonthDemo {
        private static String[] month;
        private static int subscription;
        public static void main(String[] args) {
            month = new String[]{"January","February","March","April","May","June",
                    "July","August","September","October","November","December",};
            boolean flag = true;
            char c;
            String str;
            InputStreamReader isr = new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(isr);
                    
            while(flag){
                System.out.print("请输入月份的数字: ");
                try{                str = br.readLine();    //第2次循环到这里不需要输入数据,怀疑是输入缓冲区没有清空,输入的非n字符被读取.
                    //subscription = Integer.parseInt(str)-1;
                    subscription = Integer.decode(str).intValue();
                    if(subscription<0 || subscription>11)
                     {
                         throw new MyException();
                      }
                     else
                     {
                                System.out.println((subscription+1)+"月的英文为:"+month[subscription]);
                                //flag = false;
                      }
                     }
                    catch(MyException e)
                    {
                        System.out.println(e);
                        System.out.println("退出请输入N,不退出则输入任意字符");
                        try{
                            c = (char)br.read();
                            if(c =='n' || c=='N' )
                            {
                                flag = false;
                            }
                            else{
                             br.readLine();
                            }
                                                    
                        }
                        catch(IOException ex){
                            ex.printStackTrace();
                        }                    
                    }
                catch(NumberFormatException nfe){
                 try{
                 throw new MyException();
                 }catch(MyException e)
                    {
                        System.out.println(e);
                        System.out.println("退出请输入N,不退出则输入任意字符");
                        try{
                            c = (char)br.read();
                            if(c =='n' || c=='N' )
                            {
                                flag = false;
                            }
                            else
                            {
                             br.readLine();
                            }
                            
                                                    
                        }
                        catch(IOException ex){
                            ex.printStackTrace();
                        }                    
                    }
                   
                }
                catch(IOException e)
                {
                    System.out.println("读入数据时出错~!");
                }
            }
        }
    }class MyException extends Exception{
        MyException(String str){
            super(str);
        }
        MyException(){
            super();
        }
        public String toString(){
            return "您输入的月份不合法~!";
        }
    }
      

  7.   

    两者没区别吧!你输入0后输出结果当时是正确的,只是输入y后,出现异常,而异常正是y在传进subscription = Integer.parseInt(str)-1后抛出的!
    哦,我上面的subscription = Integer.decode(str).intValue();不对,也应减一,楼主还是采用subscription = Integer.parseInt(str)-1就行
      

  8.   

    恩,现在问题聚焦在了(char)System.in.read()与(char)br.read()上面.
    为什么第1个就会出现异常,第2个就不会出现异常呢?
    能解释下吗?
      

  9.   

    这个对了!
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;public class MonthDemo {
        private static String[] month;
        private static int subscription;
        public static void main(String[] args) {
            month = new String[]{"January","February","March","April","May","June",
                    "July","August","September","October","November","December",};
            boolean flag = true;
            char c;
            String str;
            InputStreamReader isr = new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(isr);
                    
            while(flag){
                System.out.print("请输入月份的数字: ");
                try{                str = br.readLine();    //第2次循环到这里不需要输入数据,怀疑是输入缓冲区没有清空,输入的非n字符被读取.
                    //subscription = Integer.parseInt(str)-1;
                    subscription = Integer.decode(str).intValue();
                    if(subscription<=0 || subscription>=12)
                     {
                         throw new MyException();
                      }
                     else
                     {
                                System.out.println((subscription)+"月的英文为:"+month[subscription-1]);
                                //flag = false;
                      }
                     }
                    catch(MyException e)
                    {
                        System.out.println(e);
                        System.out.println("退出请输入N,不退出则输入任意字符");
                        try{
                            c = (char)br.read();
                            if(c =='n' || c=='N' )
                            {
                                flag = false;
                            }
                            else{
                             br.readLine();//读完缓冲区
                            }
                                                    
                        }
                        catch(IOException ex){
                            ex.printStackTrace();
                        }                    
                    }
                catch(NumberFormatException nfe){
                 try{
                 throw new MyException();
                 }catch(MyException e)
                    {
                        System.out.println(e);
                        System.out.println("退出请输入N,不退出则输入任意字符");
                        try{
                            c = (char)br.read();
                            if(c =='n' || c=='N' )
                            {
                                flag = false;
                            }
                            else
                            {
                             br.readLine();
                            }
                            
                                                    
                        }
                        catch(IOException ex){
                            ex.printStackTrace();
                        }                    
                    }
                   
                }
                catch(IOException e)
                {
                    System.out.println("读入数据时出错~!");
                }
            }
        }
    }class MyException extends Exception{
        MyException(String str){
            super(str);
        }
        MyException(){
            super();
        }
        public String toString(){
            return "您输入的月份不合法~!";
        }
    }
      

  10.   

    System.in()和C中的scanf()及C++中的cin>>差不多,最后还会把我们结束输入的回车当做一个字符!其实br.read()也一样,只是我加了条br.readLine(),认为的把流中的数据读出了!!
    这是我的理解,仅供参考,呵呵!