我是靠谱客的博主 认真樱桃,最近开发中收集的这篇文章主要介绍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();

}

最后

以上就是认真樱桃为你收集整理的java异常并重新输入,Java - 异常处理 - 如何重新输入无效输入的全部内容,希望文章能够帮你解决java异常并重新输入,Java - 异常处理 - 如何重新输入无效输入所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(48)

评论列表共有 0 条评论

立即
投稿
返回
顶部