英文:
How do I message an end user that they are entering the wrong input and/or format without getting an Exception/Crash?
问题
Here's the translated content:
“Eclipse IDE for Java Developers(包括孵化组件)版本:2023-03(4.27.0)”
[使用Java进行编程简介](https://math.hws.edu/javanotes/c3/s5.html) 第3.5.3节
很抱歉发布得这么长。这是我首次在任何地方谈论编程。感谢任何帮助!如果这本书不适合学习Java,我将感激任何其他推荐。
以下是我正在使用的在线教材中的内容,我在程序中遇到了异常。例如:一旦运行程序,一切似乎都正常工作,直到我输入程序不期望的格式。我希望它跳转到“else”并在输入不正确的格式时打印消息,但由于某种原因,它仍然报错。最终用户应该输入类似于“56英寸”或“156英尺”的内容,以便进行转换并以多种不同的单位显示,但如果我只输入“英寸”,它会因为我跳过了测量变量而引发下面的异常。
如何使该程序向最终用户输出合理的消息,如果他们输入不正确的格式,然后将他们带回输入说明?
```lang-java
----*错误*---
**“输入测量值,或者输入0以结束:英寸
线程“main”中的异常java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextDouble(Scanner.java:2564)
at UnitConversionTextBook.main(UnitConversionTextBook.java:22)”**
---程序---
import java.util.Scanner;
public class UnitConversionTextBook {
public static void main(String[] args) {
String units;
double measurement;
double inches, feet, yards, miles;
System.out.println(""
+ "以英寸、英尺、码或英里输入测量值。\n"
+ "一些示例:1英寸或17英尺或2.73英里\n"
+ "您可以使用缩写:in ft yd mi\n"
+ "在输入测量值后,请按回车键。\n"
+ "我还会将您的输入转换为其他度量单位。\n"
+ "");
while (true) {
System.out.print("输入您的测量值,或输入0以结束:");
Scanner stdio = new Scanner(System.in);
measurement = stdio.nextDouble();
units = stdio.next();
units = units.toLowerCase();
if (measurement == 0) {
break;
}
if (units.equals("inch") || units.equals("inches")|| units.equals("in")) {
inches = measurement;
}
else if (units.equals("foot") || units.equals("feet")|| units.equals("ft")) {
inches = measurement * 12;
}
else if (units.equals("yard") || units.equals("yards")|| units.equals("yd")) {
inches = measurement * 36;
}
else if (units.equals("mile") || units.equals("miles")|| units.equals("mi")) {
inches = measurement * 12 * 5280;
}
else {
System.out.println("抱歉,我不明白\"" + units + "\"。");
continue;
}
feet = inches / 12;
yards = inches / 36;
miles = inches / (12*5280);
System.out.println();
System.out.printf(""
+ "相当于:\n"
+ "%14.5g英寸\n"
+ "%14.5g英尺\n"
+ "%14.5g码\n"
+ "%14.5g英里\n"
+ "", inches, feet, yards, miles);
System.out.println();
}
}
}
我尝试在while语句中输入参数,还尝试了其他愚蠢的方法,但我刚刚开始学习,所以分享没有太大意义。我希望有些东西能够像这样工作:
如果最终用户没有先输入数字测量值,然后是单位测量类型,那么向用户发送消息,告诉他们未按照合理规定输入,请按照已合理规定的格式输入。
第一个输入应该是double(测量值)
第二个输入应该是字符串(单位)
这本书的编码方式是它们都应该在同一行上输入,然后按回车键。
Please note that the code and text contain HTML-encoded characters like `"`, which are used for double quotes. If you need assistance with any specific part of the translation or have further questions, feel free to ask.
<details>
<summary>英文:</summary>
"Eclipse IDE for Java Developers (includes Incubating components) Version: 2023-03 (4.27.0)"
[Intro to Programming using Java](https://math.hws.edu/javanotes/c3/s5.html) Section 3.5.3
Sorry for the very long post. This is my first post anywhere about programming. Thanks for any help! I would appreciate any other recommendations if this book is not a good one to learn java from.
The below is from the above online textbook that I am using to try an learn Java. I have issues with Exceptions in the program. For Example: Once running the program, everything seems to be working fine until I enter a format that the program does not expect. I want it to skip to "else" and print the message if an incorrect format is entered, but for some reason it just Excepts out. The end user is suppose to enter something like "56 inches" or "156 feet" so that it can be converted and display in multiple different units, but if I just enter "inches" it gives me the blow exception because I skipped the measurement variable.
How do I make this program output a reasonable message to the end user, if they are entering the incorrect format that afterwards places them back at the input directions?
```lang-java
----*Error*---
**"Enter your measurement, or 0 to end: inches
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextDouble(Scanner.java:2564)
at UnitConversionTextBook.main(UnitConversionTextBook.java:22)"**
---Program---
import java.util.Scanner;
public class UnitConversionTextBook {
public static void main(String[] args) {
String units;
double measurement;
double inches, feet, yards, miles;
System.out.println("""
Enter measurements in inches, feet, yards, or miles.
Some examples: 1 inch or 17 feet or 2.73 miles
You can use the abbreviations: in ft yd mi
Please press enter after typing your measurement.
I will also convert your input into the other units
of measurements.
""");
while (true) {
System.out.print("Enter your measurement, or 0 to end: ");
Scanner stdio = new Scanner(System.in);
measurement = stdio.nextDouble();
units = stdio.next();
units = units.toLowerCase();
if (measurement == 0) {
break;
}
if (units.equals("inch") || units.equals("inches")|| units.equals("in")) {
inches = measurement;
}
else if (units.equals("foot") || units.equals("feet")|| units.equals("ft")) {
inches = measurement * 12;
}
else if (units.equals("yard") || units.equals("yards")|| units.equals("yd")) {
inches = measurement * 36;
}
else if (units.equals("mile") || units.equals("miles")|| units.equals("mi")) {
inches = measurement * 12 * 5280;
}
else {
System.out.println("Sorry, but I don’t understand \""
+ units + "\".");
continue;
}
feet = inches / 12;
yards = inches / 36;
miles = inches / (12*5280);
System.out.println();
System.out.printf("""
That’s equivalent to:
%14.5g inches
%14.5g feet
%14.5g yards
%14.5g miles
""", inches, feet, yards, miles);
System.out.println();
}
}
}
I have tried entering parameters into the while statement, other silly attempts, but I am just starting out so it is pretty pointless to share. I would like for something to work like
if end user does not enter numerical measurement first followed by unit measurement type then
message user they are not following directions and to please enter the input with the correct format that has been reasonably dictated.
first input should be a double(measurement)
second input should be string(unit)
the way this book codes it is they are both to be inputted on the same line followed by pressing the enter key.
答案1
得分: 1
Way 1: 高级别修复方法有两种
方法1:您需要将逻辑包装在try-catch块中。在这里,需要进行InputMismatchException处理,因为您期望的是double,但实际输入可能是其他类型,您可能还有其他情况需要使用多重捕获。
import java.util.InputMismatchException;
import java.util.Scanner;
public class UnitConversionTextBook {
public static void main(String[] args) {
String units;
double measurement;
double inches, feet, yards, miles;
System.out.println(
"Enter measurements in inches, feet, yards, or miles. Some examples: 1 inch or 17 feet or 2.73 miles. You can use the abbreviations: in ft yd mi. Please press enter after typing your measurement. I will also convert your input into the other units of measurements.");
while (true) {
try {
System.out.print("Enter your measurement, or 0 to end: ");
Scanner stdio = new Scanner(System.in);
measurement = stdio.nextDouble();
units = stdio.next();
units = units.toLowerCase();
if (measurement == 0) {
break;
}
if (units.equals("inch") || units.equals("inches") || units.equals("in")) {
inches = measurement;
} else if (units.equals("foot") || units.equals("feet") || units.equals("ft")) {
inches = measurement * 12;
} else if (units.equals("yard") || units.equals("yards") || units.equals("yd")) {
inches = measurement * 36;
} else if (units.equals("mile") || units.equals("miles") || units.equals("mi")) {
inches = measurement * 12 * 5280;
} else {
System.out.println("Sorry, but I don’t understand \"" + units + "\".");
continue;
}
feet = inches / 12;
yards = inches / 36;
miles = inches / (12 * 5280);
System.out.println();
System.out.printf("That’s equivalent to: %14.5g inches %14.5g feet %14.5g yards %14.5g miles", inches,
feet, yards, miles);
System.out.println();
} catch (InputMismatchException e) {
// TODO Auto-generated catch block
System.out.println("Please read instructions again and retry");
continue;
}
}
}
}
方法2:只在try-catch块中包装双精度读取代码,但记得要对本地变量进行初始化。
public class UnitConversionTextBook {
public static void main(String[] args) {
String units;
double measurement = 0;
double inches, feet, yards, miles;
System.out.println(
"Enter measurements in inches, feet, yards, or miles. Some examples: 1 inch or 17 feet or 2.73 miles. You can use the abbreviations: in ft yd mi. Please press enter after typing your measurement. I will also convert your input into the other units of measurements.");
while (true) {
System.out.print("Enter your measurement, or 0 to end: ");
Scanner stdio = new Scanner(System.in);
try {
measurement = stdio.nextDouble();
} catch (InputMismatchException e) {
// TODO Auto-generated catch block
System.out.println("Please read instructions again and retry");
continue;
}
units = stdio.next();
units = units.toLowerCase();
if (measurement == 0) {
break;
}
if (units.equals("inch") || units.equals("inches") || units.equals("in")) {
inches = measurement;
} else if (units.equals("foot") || units.equals("feet") || units.equals("ft")) {
inches = measurement * 12;
} else if (units.equals("yard") || units.equals("yards") || units.equals("yd")) {
inches = measurement * 36;
} else if (units.equals("mile") || units.equals("miles") || units.equals("mi")) {
inches = measurement * 12 * 5280;
} else {
System.out.println("Sorry, but I don’t understand \"" + units + "\".");
continue;
}
feet = inches / 12;
yards = inches / 36;
miles = inches / (12 * 5280);
System.out.println();
System.out.printf("That’s equivalent to: %14.5g inches %14.5g feet %14.5g yards %14.5g miles", inches,
feet, yards, miles);
System.out.println();
}
}
}
英文:
There are two ways at high level to fix this
Way 1: You need to wrap your logic inside a try-catch. Here it needs the InputMismatchException handling as you are expecting double but it is coming as something else you may have other scenarios as well where you can make it multi-catch.
import java.util.InputMismatchException;
import java.util.Scanner;
public class UnitConversionTextBook {
public static void main(String[] args) {
String units;
double measurement;
double inches, feet, yards, miles;
System.out.println(
"Enter measurements in inches, feet, yards, or miles.Some examples: 1 inch or 17 feet or 2.73 miles You can use the abbreviations: in ft yd mi Please press enter after typing your measurement. I will also convert your input into the other units of measurements.");
while (true) {
try {
System.out.print("Enter your measurement, or 0 to end: ");
Scanner stdio = new Scanner(System.in);
measurement = stdio.nextDouble();
units = stdio.next();
units = units.toLowerCase();
if (measurement == 0) {
break;
}
if (units.equals("inch") || units.equals("inches") || units.equals("in")) {
inches = measurement;
} else if (units.equals("foot") || units.equals("feet") || units.equals("ft")) {
inches = measurement * 12;
} else if (units.equals("yard") || units.equals("yards") || units.equals("yd")) {
inches = measurement * 36;
} else if (units.equals("mile") || units.equals("miles") || units.equals("mi")) {
inches = measurement * 12 * 5280;
} else {
System.out.println("Sorry, but I don’t understand \"" + units + "\".");
continue;
}
feet = inches / 12;
yards = inches / 36;
miles = inches / (12 * 5280);
System.out.println();
System.out.printf("That’s equivalent to: %14.5g inches %14.5g feet %14.5g yards %14.5g miles", inches,
feet, yards, miles);
System.out.println();
} catch (InputMismatchException e) {
// TODO Auto-generated catch block
System.out.println("Please read instructions again and retry");
continue;
}
}
}
}
Way 2:
Wrap only double reading code in try - catch
But remember to initilize a value to local variable
public class UnitConversionTextBook {
public static void main(String[] args) {
String units;
double measurement=0;
double inches, feet, yards, miles;
System.out.println(
"Enter measurements in inches, feet, yards, or miles.Some examples: 1 inch or 17 feet or 2.73 miles You can use the abbreviations: in ft yd mi Please press enter after typing your measurement. I will also convert your input into the other units of measurements.");
while (true) {
System.out.print("Enter your measurement, or 0 to end: ");
Scanner stdio = new Scanner(System.in);
try {
measurement = stdio.nextDouble();
} catch (InputMismatchException e) {
// TODO Auto-generated catch block
System.out.println("Please read instructions again and retry");
continue;
}
units = stdio.next();
units = units.toLowerCase();
if (measurement == 0) {
break;
}
if (units.equals("inch") || units.equals("inches") || units.equals("in")) {
inches = measurement;
} else if (units.equals("foot") || units.equals("feet") || units.equals("ft")) {
inches = measurement * 12;
} else if (units.equals("yard") || units.equals("yards") || units.equals("yd")) {
inches = measurement * 36;
} else if (units.equals("mile") || units.equals("miles") || units.equals("mi")) {
inches = measurement * 12 * 5280;
} else {
System.out.println("Sorry, but I don’t understand \"" + units + "\".");
continue;
}
feet = inches / 12;
yards = inches / 36;
miles = inches / (12 * 5280);
System.out.println();
System.out.printf("That’s equivalent to: %14.5g inches %14.5g feet %14.5g yards %14.5g miles", inches,
feet, yards, miles);
System.out.println();
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论