我的扫描仪类 java 代码有一个异常是错误

运行我的代码时出现此错误,请帮助。

线程“main”中的异常 java.util.InputMismatchException

我将不胜感激您可以为整个代码提供的任何修复。

在这种情况下,当我输入重量等数据时,它充满了错误并且很烦人。

错误信息:


run:

Input name: 

Test

Input weight: 

90

10

Name: Test

weight : 90.0

Input name: 

Input weight: 

Test1

Exception in thread "main" java.util.InputMismatchException

    at java.util.Scanner.throwFor(Scanner.java:864)

    at java.util.Scanner.next(Scanner.java:1485)

    at java.util.Scanner.nextDouble(Scanner.java:2413)

    at howto.Howto.main(Howto.java:45)

Java Result: 1

BUILD SUCCESSFUL (total time: 16 seconds)


长风秋雁
浏览 179回答 2
2回答

qq_笑_17

有几个问题很突出……第一的...Scanner sc1 = new Scanner(System.in);Scanner sc2 = new Scanner(System.in);您不需要多个扫描仪,无论如何它们都从同一个流中读取,最好只使用一个并降低复杂性。下一个...String scanner1 = sc1.nextLine();name [i] = scanner1;System.out.println("Input weight: ");double scanner2 = sc2.nextDouble();if(!sc1.hasNextDouble()){    System.out.println("Invalid Weight!. Start Again");} else{    weightkg[i] =  scanner2;}使用时nextDouble,缓冲区仍包含换行符,这意味着下次使用时nextLine,它将返回空白String并继续。此外,hasNextDouble似乎在等待数据,但您已经double从缓冲区中读取了值,留下了悬空的新行。在我的测试中,这导致程序等待更多输入出现问题。我通过做这样的事情“解决”了基本问题......String scanner1 = sc1.nextLine();name [i] = scanner1;System.out.println("Input weight: ");double scanner2 = sc1.nextDouble();weightkg[i] =  scanner2;sc1.nextLine();现在这个“将”起作用,但这不是最好的解决方案。“不同”的方法可能是将权重作为 a 读取String并尝试将其解析为 a double,这使您有机会捕获无效输入并以更合适的方式处理它,例如......System.out.println("Input name: ");String scanner1 = sc1.nextLine();name[i] = scanner1;boolean done = false;double weight = 0;do {    System.out.println("Input weight: ");    String input = sc1.nextLine();    try {        weight = Double.parseDouble(input);        done = true;    } catch (NumberFormatException nfe) {        System.out.println("!! Invalid value");    }} while (!done);weightkg[i] = weight;System.out.println("Name: " + name[i]);System.out.println("weight : " + weightkg[i]);}

三国纷争

你的代码中有一些逻辑错误。在每一行之后我都会提到它们。import java.util.Scanner;public class HowTo {&nbsp; public static void main(String[] args) {&nbsp; &nbsp; Scanner sc1 = new Scanner(System.in); // you need only 1 scanner&nbsp; &nbsp; double weightkg[] = new double[30];&nbsp; &nbsp; double weightkgEndOfMonth[] = new double[30];&nbsp; &nbsp; String name[] = new String[30];&nbsp; &nbsp; double weightDifference[] = new double[30];&nbsp; &nbsp; for (int i = 0; i < 30; i++) { // need to iterate from 0 index to 29&nbsp; &nbsp; &nbsp; System.out.print("Input name: ");&nbsp; &nbsp; &nbsp; String scanner1 = sc1.nextLine();&nbsp; &nbsp; &nbsp; name[i] = scanner1;&nbsp; &nbsp; &nbsp; System.out.print("Input weight: ");&nbsp; &nbsp; &nbsp; if (!sc1.hasNextDouble()) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Invalid Weight!. Start Again");&nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; weightkg[i] = sc1.nextDouble();// if it has double then read it&nbsp;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; System.out.println("Name: " + name[i]);&nbsp; &nbsp; &nbsp; System.out.println("weight : " + weightkg[i]);&nbsp; &nbsp; &nbsp; sc1.nextLine();&nbsp; &nbsp; }&nbsp; &nbsp; for (int i = 0; i < 30; i++) {// need to iterate from 0 index to 29&nbsp; &nbsp; &nbsp; System.out.println("Input weight at the end of month: ");&nbsp; &nbsp; &nbsp; if (!sc1.hasNextDouble()) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Invalid Weight!. Start Again");&nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; weightkgEndOfMonth[i] = sc1.nextDouble();// read double here&nbsp;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; weightDifference[i] =weightkgEndOfMonth[i]- weightkg[i] ;// weight difference is (final weight- initial weight)&nbsp; &nbsp; &nbsp; if (weightDifference[i] > 2.5) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Student with a weight difference greater than 2.5kg: " + name[i]);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Weight difference: " + weightDifference[i]);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Rise");&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; if (weightDifference[i] < -2.5) {// fall if weight less than 2.5&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Student with a weight difference greater than 2.5kg: " + name[i]);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Weight difference: " + weightDifference[i]);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Fall");&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; }}现在它工作正常。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java