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

时间:2018-10-21 23:57:25

标签: java

运行代码时出现此错误,请帮忙。

  

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

对于您可以为代码提供的所有修复程序,我将不胜感激。

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

package howto;

import java.util.Scanner;


public class Howto {


    public static void main(String[] args) {
        Scanner sc1 = new Scanner(System.in);

        Scanner sc2 = new Scanner(System.in);

        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<31; i++)

        {

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

            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;

            }



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

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



        }



        for (int i = 0; i<31; i++)

        {



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

            double scanner2 = sc2.nextDouble();



            if(!sc1.hasNextDouble())

            {

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

            } else

            {

                weightkgEndOfMonth[i] =  scanner2;

            }



            weightDifference [i] = weightkg[i] - weightkgEndOfMonth[i];



            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)

            {

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

            }

        }

    }

}

错误消息:

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)

2 个答案:

答案 0 :(得分:1)

有一些突出的问题...

首先...

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

现在,这种“将”有效,但这不是最佳解决方案。一种“不同”的方法可能是将权重读为String并尝试将其解析为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]);

}

答案 1 :(得分:0)

您的代码中存在一些逻辑错误。在每一行之后我都提到他们。

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");
      }

    }

  }
}

现在一切正常。