注释部分为问题所在.如何能让第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]
输入数据如下:
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]
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,你在第一个try那里捕捉的是IOException,也就是说,如果你输入数字n不能被Integer.parseInt(n),NumberFormatException这个异常你就捕获
不到,可以改成Exception
自己把逻辑再整理下,很简单就能改好
第2.我现在不是要捕获NumberFormatException.
最后,还是感谢你的回帖.
另外,流中的数据不会存在于缓冲区的,readLIne()已经读出了!
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 "您输入的月份不合法~!";
}
}
哦,我上面的subscription = Integer.decode(str).intValue();不对,也应减一,楼主还是采用subscription = Integer.parseInt(str)-1就行
为什么第1个就会出现异常,第2个就不会出现异常呢?
能解释下吗?
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 "您输入的月份不合法~!";
}
}
这是我的理解,仅供参考,呵呵!