java.util.inputmismatchexception

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

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

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

  1. import java.util.Scanner;
  2. import java.io.*;
  3. public class GardinierPayrollP2
  4. {
  5. public static void main(String[] args) throws IOException
  6. {
  7. // int id; //i.d. number
  8. // double hrsworkd; //hours worked
  9. double wkspay = 0.00; //total amount before tax for an individual employee
  10. double netpay = 0.00; //net pay to an individual after tax
  11. double runningTotal = 0.00; //total amount of paid salaries
  12. double runningNetTotal = 0.00; //net total after tax
  13. double runningTaxTotal = 0.00; //total taxes payed
  14. double levelA = 12.00;
  15. double levelB = 14.50;
  16. double levelC = 16.00;
  17. double levelD = 20.00;
  18. //String name; //employees First Name
  19. //char level; //level of payment for employee
  20. final double taxRate = .08; //tax rate @ 8%
  21. double taxes = 0.00; //dollar amount of taxes payed
  22. File employees = new File("employees.txt");
  23. Scanner inputFile = new Scanner(employees);
  24. while (inputFile.hasNext())
  25. {
  26. String name = inputFile.nextLine();
  27. int id = inputFile.nextInt();
  28. char level = inputFile.next().charAt(0);
  29. double hrsworkd = inputFile.nextDouble();
  30. System.out.println(name + id + level + hrsworkd);
  31. }
  32. }
  33. }

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

  1. > run GardinierPayrollP2
  2. Rose Nylund901A10.0
  3. java.util.InputMismatchException
  4. at java.util.Scanner.throwFor(Unknown Source)
  5. at java.util.Scanner.next(Unknown Source)
  6. at java.util.Scanner.nextInt(Unknown Source)
  7. at java.util.Scanner.nextInt(Unknown Source)
  8. at GardinierPayrollP2.main(GardinierPayrollP2.java:33)
  9. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  10. at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
  11. at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  12. at java.lang.reflect.Method.invoke(Unknown Source)
  13. at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
  14. >

这是输入文件

  1. Rose Nylund
  2. 901
  3. A
  4. 10.0
  5. Dorothy Zbornak
  6. 534
  7. D
  8. 11.5
  9. Blanche Deveraux
  10. 109
  11. B
  12. 5.0
  13. Sophia Petrillo
  14. 729
  15. C
  16. 2.5

我做错什么了|

5cnsuln7

5cnsuln71#

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

  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.util.Scanner;
  4. public class Main {
  5. public static void main(String[] args) throws FileNotFoundException {
  6. File employees = new File("employees.txt");
  7. Scanner inputFile = new Scanner(employees);
  8. String name = null;
  9. int id = 0;
  10. char level = 0;
  11. double hrsworkd = 0;
  12. while (inputFile.hasNextLine()) {
  13. name = inputFile.nextLine();
  14. if (inputFile.hasNextLine()) {
  15. id = Integer.parseInt(inputFile.nextLine());
  16. }
  17. if (inputFile.hasNextLine()) {
  18. level = inputFile.nextLine().charAt(0);
  19. }
  20. if (inputFile.hasNextLine()) {
  21. hrsworkd = Double.parseDouble(inputFile.nextLine());
  22. }
  23. System.out.println(name + "," + id + "," + level + "," + hrsworkd);
  24. }
  25. inputFile.close();
  26. }
  27. }

输出:

  1. Rose Nylund,901,A,10.0
  2. Dorothy Zbornak,534,D,11.5
  3. Blanche Deveraux,109,B,5.0
  4. Sophia Petrillo,729,C,2.5
展开查看全部

相关问题