为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

长风秋雁 2021-12-01 15:26:51
运行我的代码时出现此错误,请帮助。线程“main”中的异常 java.util.InputMismatchException我将不胜感激您可以为整个代码提供的任何修复。在这种情况下,当我输入重量等数据时,它充满了错误并且很烦人。错误信息:run:Input name: TestInput weight: 9010Name: Testweight : 90.0Input name: Input weight: Test1Exception 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: 1BUILD SUCCESSFUL (total time: 16 seconds)

2 回答

?
qq_笑_17

TA贡献1818条经验 获得超7个赞

有几个问题很突出……


第一的...


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]);

}


查看完整回答
反对 回复 2021-12-01
?
三国纷争

TA贡献1804条经验 获得超7个赞

你的代码中有一些逻辑错误。在每一行之后我都会提到它们。


import java.util.Scanner;


public class HowTo {


  public static void main(String[] args) {

    Scanner sc1 = new Scanner(System.in); // you need only 1 scanner

    double weightkg[] = new double[30];

    double weightkgEndOfMonth[] = new double[30];

    String name[] = new String[30];

    double weightDifference[] = new double[30];

    for (int i = 0; i < 30; i++) { // need to iterate from 0 index to 29

      System.out.print("Input name: ");

      String scanner1 = sc1.nextLine();

      name[i] = scanner1;

      System.out.print("Input weight: ");

      if (!sc1.hasNextDouble()) {

        System.out.println("Invalid Weight!. Start Again");

      } else {

        weightkg[i] = sc1.nextDouble();// if it has double then read it 

      }

      System.out.println("Name: " + name[i]);

      System.out.println("weight : " + weightkg[i]);

      sc1.nextLine();

    }

    for (int i = 0; i < 30; i++) {// need to iterate from 0 index to 29

      System.out.println("Input weight at the end of month: ");

      if (!sc1.hasNextDouble()) {

        System.out.println("Invalid Weight!. Start Again");

      } else {

        weightkgEndOfMonth[i] = sc1.nextDouble();// read double here 

      }

      weightDifference[i] =weightkgEndOfMonth[i]- weightkg[i] ;// weight difference is (final weight- initial weight)

      if (weightDifference[i] > 2.5) {

        System.out.println("Student with a weight difference greater than 2.5kg: " + name[i]);

        System.out.println("Weight difference: " + weightDifference[i]);

        System.out.println("Rise");

      }

      if (weightDifference[i] < -2.5) {// fall if weight less than 2.5

        System.out.println("Student with a weight difference greater than 2.5kg: " + name[i]);

        System.out.println("Weight difference: " + weightDifference[i]);

        System.out.println("Fall");

      }


    }


  }

}

现在它工作正常。


查看完整回答
反对 回复 2021-12-01
  • 2 回答
  • 0 关注
  • 178 浏览

添加回答

代码语言

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号