java.util.inputmismatchexception

gorkyyrv  于 2021-07-11  发布在  Java
关注(0)|答案(1)|浏览(415)

这个问题在这里已经有答案了

为什么输入不匹配(5个答案)
上个月关门了。

import java.util.Scanner;
import java.io.*;
public class GardinierPayrollP2
{
  public static void main(String[] args) throws IOException
 {
  // int id;  //i.d. number
  // double hrsworkd; //hours worked
   double wkspay = 0.00;  //total amount before tax for an individual employee
   double netpay = 0.00; //net pay to an individual after tax
   double runningTotal = 0.00; //total amount of paid salaries
   double runningNetTotal = 0.00; //net total after tax 
   double runningTaxTotal = 0.00; //total taxes payed 
   double levelA = 12.00;
   double levelB = 14.50;
   double levelC = 16.00;
   double levelD = 20.00;
   //String name; //employees First Name
   //char level; //level of payment for employee
   final double taxRate = .08; //tax rate @ 8%
   double taxes = 0.00; //dollar amount of taxes payed

  File employees = new File("employees.txt");
  Scanner inputFile = new Scanner(employees);

  while (inputFile.hasNext())
  {

   String name = inputFile.nextLine();
   int id = inputFile.nextInt();
   char level = inputFile.next().charAt(0);
   double hrsworkd = inputFile.nextDouble();

   System.out.println(name + id + level + hrsworkd);
  }

 }
}

这就是我的工作。计算机科学二年级。还不太精通故障排除。我知道有输入错误。我就是不知道为什么。drjava的输出是打印信息的前四行,然后抛出错误。这是输出

> run GardinierPayrollP2
Rose Nylund901A10.0
java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at GardinierPayrollP2.main(GardinierPayrollP2.java:33)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
>

这是输入文件

Rose Nylund
901
A
10.0
Dorothy Zbornak
534
D
11.5
Blanche Deveraux
109
B
5.0
Sophia Petrillo
729
C
2.5

我做错什么了|

5cnsuln7

5cnsuln71#

一定要关门 inputFile 在处理结束时。
不要混合 inputFile.hasNext()inputFile.nextLine() 以及 inputFile.nextInt() i、 如果你想使用 inputFile.nextLine() ,则应测试其相应的 hasNextXXXinputFile.hasNextLine() . 另外,我会用 inputFile.nextLine() 并将行解析为 int 或者 double 因为这里讨论的问题,所以按要求。
演示:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws FileNotFoundException {
        File employees = new File("employees.txt");
        Scanner inputFile = new Scanner(employees);
        String name = null;
        int id = 0;
        char level = 0;
        double hrsworkd = 0;
        while (inputFile.hasNextLine()) {
            name = inputFile.nextLine();
            if (inputFile.hasNextLine()) {
                id = Integer.parseInt(inputFile.nextLine());
            }
            if (inputFile.hasNextLine()) {
                level = inputFile.nextLine().charAt(0);
            }

            if (inputFile.hasNextLine()) {
                hrsworkd = Double.parseDouble(inputFile.nextLine());
            }
            System.out.println(name + "," + id + "," + level + "," + hrsworkd);
        }
        inputFile.close();
    }
}

输出:

Rose Nylund,901,A,10.0
Dorothy Zbornak,534,D,11.5
Blanche Deveraux,109,B,5.0
Sophia Petrillo,729,C,2.5

相关问题