java异常并重新输入,Java - 异常处理 - 如何重新输入无效输入

The exception handling only accepts double inputs. Therefore when the user enters "k" for example, it will say "Error! Please enter a number!". However instead of allowing the user to re-enter the input, it jumps onto the next input "Average impulse) How can I make it work so it will stay on the same line and allow to re-enter a value?

//Main class

public class Main { //Master class

public static void main( String args[] ) //Standard header for main method

{

kbentry input = new kbentry(); //Creates object of kbentry class

System.out.print("\nPlease enter a number for Total Impulse: " ); //Print message to enter 1. input

double totalImpulse = input.totalImpulse1(); //Holds the variable entered

System.out.println("You have entered : " + totalImpulse); //Shows the variable entered

System.out.print("\nPlease enter a number for Average Impulse: " ); //Print message to enter 2. input

double averageImpulse = input.averageImpulse2(); //Holds the variable entered

System.out.println("You have entered : " + averageImpulse); //Shows the variable entered

}

}

//kbentry class

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.io.IOException;

public class kbentry{ //Class name

double totalImpulse1(){ //Method for 1. input

BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); //Creates BufferedReader object for System.in

//Total Impulse entry

String strTotalImpulse = null; // These must be initialised

double intTotalImpulse = 0; //Setting it double

try {

strTotalImpulse = in.readLine(); //Reads string value from the keyboard

}

catch (IOException ioe) { // ignore exception

}

try {

intTotalImpulse = Double.parseDouble(strTotalImpulse); // convert it to double

}

catch (NumberFormatException nfe) {

System.out.println("Error! Please enter a number!" + nfe.toString()); //Error message if its not a double

}

return intTotalImpulse; //return value

}

double averageImpulse2(){ //Method for 2. input

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));//Creates BufferedReader object for System.in

// String for AverageImpulse

String strAverageImpulse = null; // These must be initialised

double intAverageImpulse = 0; //Setting it double

try {

strAverageImpulse = in.readLine(); //Reads string value from the keyboard

}

catch (IOException ioe) { // ignore exception

}

// convert it to integer

try {

intAverageImpulse = Double.parseDouble(strAverageImpulse); // convert it to double

}

catch (NumberFormatException nfe) {

System.out.println("Error! Please enter a number!" + nfe.toString()); //Error message if its not a double

}

return intAverageImpulse; //return value

}

}

解决方案

If you user enter other than double you will get NumberFormatException in that cause you have to simply call that method again in you

catch (NumberFormatException nfe) {

System.out.println("Error! Please enter a number!" + nfe.toString()); //Error message if its not a double

//again ask for input

System.out.print("\nPlease enter a number for Total Impulse: ");

return totalImpulse1();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值