使用Scanner从.txt文件读取时的InputMismatchException

时间:2015-03-11 10:26:41

标签: java java.util.scanner

我收到了错误:

Exception in thread "main" java.util.InputMismatchException
 at java.util.Scanner.throwFor(Unknown Source)
.....
 at Teng_Grading.validateData(Teng_Grading.java:52)
 at Teng_Grading.main(Teng_Grading.java:838)

我认为我在第二种方法中做错了但我找不到什么。 另外,我不允许在这个程序中使用数组。

public static void validateData(File inputFile) throws IOException
{
  inputFile = new File("grading.txt");

  File inputFile1 = new File("grading.txt");
  Scanner readInput = new Scanner(inputFile);
  Scanner readInput1 = new Scanner(inputFile1);
  PrintWriter pw = new PrintWriter("invalidID.txt");
  PrintWriter pw2 = new PrintWriter("invalidEC.txt");
  PrintWriter pw3 = new PrintWriter("invalidPercent.txt");
  PrintWriter pw4 = new PrintWriter("validID.txt");
  PrintWriter pw5 = new PrintWriter("validEC.txt");
  PrintWriter pw6 = new PrintWriter("validPercent.txt");

  int iD1 = readInput1.nextInt();
  int eC1 = readInput1.nextInt();
  double percent1 = readInput1.nextDouble();
  int iD2 = readInput1.nextInt();
  int eC2 = readInput1.nextInt();
  double percent2 = readInput1.nextDouble();
  int iD3 = readInput1.nextInt();
  int eC3 = readInput1.nextInt();
  double percent3 = readInput1.nextDouble();
  int iD4 = readInput1.nextInt();
  int eC4 = readInput1.nextInt();
  double percent4 = readInput1.nextDouble();
  int iD5 = readInput1.nextInt();
  int eC5 = readInput1.nextInt();
  double percent5 = readInput1.nextDouble();
  int iD6 = readInput1.nextInt();
  int eC6 = readInput1.nextInt();
  double percent6 = readInput1.nextDouble();
  int iD7 = readInput1.nextInt();
  int eC7 = readInput1.nextInt();
  double percent7 = readInput1.nextDouble();
  int iD8 = readInput1.nextInt();
  int eC8 = readInput1.nextInt();
  double percent8 = readInput1.nextDouble();
  int iD9 = readInput1.nextInt();
  int eC9 = readInput1.nextInt();
  double percent9 = readInput.nextDouble();
  int iD10 = readInput1.nextInt();
  int eC10 = readInput1.nextInt();
  double percent10 = readInput1.nextDouble();
  int iD11 = readInput1.nextInt();
  int eC11 = readInput1.nextInt();
  double percent11 = readInput1.nextDouble();

  if(iD1 < 00000 || iD1 > 99999)
    pw.println(""+readInput.next());
  else
    pw4.println(""+readInput.next());

  if(eC1 > 5 || eC1 < 0)
    pw2.println(""+readInput.next());
  else
    pw5.println(""+readInput.next());

  if(percent1 > 100.00 || percent1 < 0.00)
    pw3.println(""+readInput.next());
  else
    pw6.println(""+readInput.next());

  if(iD2 < 00000 || iD2 > 99999)
    pw.println(""+readInput.next());
  else
    pw4.println(""+readInput.next());

  if(eC2 > 5 || eC2 < 0)
    pw2.println(""+readInput.next());
  else
    pw5.println(""+readInput.next());

  if(percent2 > 100.00 || percent2 < 0.00)
    pw3.println(""+readInput.next());
  else
    pw6.println(""+readInput.next());

  if(iD3 < 00000 || iD3 > 99999)
    pw.println(""+readInput.next());
  else
    pw4.println(""+readInput.next());

  if(eC3 > 5 || eC3 < 0 )
    pw2.println(""+readInput.next());
  else
    pw5.println(""+readInput.next());

  if(percent3 > 100.00 || percent3 < 0.00)
    pw3.println(""+readInput.next());
  else
    pw6.println(""+readInput.next());

  if(iD4 < 00000 || iD4 > 99999)
    pw.println(""+readInput.next());
  else
    pw4.println(""+readInput.next());

  if(eC4 > 5 || eC4 < 0 )
    pw2.println(""+readInput.next());
  else
    pw5.println(""+readInput.next());

  if(percent4 > 100.00 || percent4 < 0.00)
    pw3.println(""+readInput.next());
  else
    pw6.println(""+readInput.next());

  if(iD5<00000 || iD5>99999)
    pw.println(""+readInput.next());
  else
    pw4.println(""+readInput.next());

  if(eC5>5 || eC5 <0)
    pw2.println(""+readInput.next());
  else
    pw5.println(""+readInput.next());

  if(percent5 > 100.00 || percent5 <0.00)
    pw3.println(""+readInput.next());
  else
    pw6.println(""+readInput.next());

  if(iD6 < 00000 || iD6>99999)
    pw.println(""+readInput.next());
  else
    pw4.println(""+readInput.next());

  if(eC6 > 5 || eC6 <0)
    pw2.println(""+readInput.next());
  else
    pw5.println(""+readInput.next());

  if(percent6 > 100.00 || percent6 < 0.00)
    pw3.println(""+readInput.next());
  else
    pw6.println(""+readInput.next());

  if(iD7 <00000 || iD7 > 99999)
    pw.println(""+readInput.next());
  else
    pw4.println(""+readInput.next());

  if(eC7 > 5 || eC7 < 0 )
    pw2.println(""+readInput.next());
  else
    pw5.println(""+readInput.next());

  if(percent7 > 100.00 || percent7 < 0.00)
    pw3.println(""+readInput.next());
  else
    pw6.println(""+readInput.next());

  if(iD8 < 00000 || iD8 >99999)
    pw.println(""+readInput.next());
  else
   pw4.println(""+readInput.next());

  if(eC8 > 5 || eC8 < 0 )
    pw2.println(""+readInput.next());
  else
    pw5.println(""+readInput.next());

  if(percent8 > 100.00 || percent8 <0.00)
    pw3.println(""+readInput.next());
  else
    pw6.println(""+readInput.next());

  if(iD9 < 00000 || iD9 > 99999)
    pw.println(""+readInput.next());
  else
    pw4.println(""+readInput.next());

  if(eC9 > 5 || eC9 < 0)
    pw2.println(""+readInput.next());
  else
    pw5.println(""+readInput.next());

  if(percent9>100.00 || percent9 < 0.00)
    pw3.println(""+readInput.next());
  else
    pw6.println(""+readInput.next());

  if(iD10 <00000 || iD10 >99999)
    pw.println(""+readInput.next());
  else
    pw4.println(""+readInput.next());

  if(eC10 >5 || eC10 < 0 )
    pw2.println(""+readInput.next());
  else
    pw5.println(""+readInput.next());

  if(percent10 > 100.00 || percent10 <0.00)
    pw3.println(""+readInput.next());
  else
    pw6.println(""+readInput.next());

  if(iD11 <00000 || iD11 >99999)
    pw.println(""+readInput.next());
  else
   pw4.println(""+readInput.next());

  if(eC11 >5 || eC11 <0 )
    pw2.println(""+readInput.next());
  else
    pw5.println(""+readInput.next());

  if(percent11 >100.00 || percent11 < 0.00)
    pw3.println(""+readInput.next());
  else
    pw6.println(""+readInput.next());

  pw.close();
  pw2.close();
  pw3.close();
  pw4.close();
  pw5.close();
  pw6.close();
}

46813 5 100.00

80795 4 86.52

34961 3 77.52

40059 2 85.61

80463 1 70.16

64088 0 80.52

65400 6 90.23

80775 2 70.16

573S1 3 83.03

21076 4 54.47

32911 5 -10.90

这是我正在尝试阅读的.txt文件

1 个答案:

答案 0 :(得分:1)

您尝试阅读的其中一个代币中有一个“S”。它将被读取为字符串,而Scanner#nextInt()需要一个整数。如果S应该在那里,请使用“Scanner#next()”,否则请仔细检查文件中的值。

编辑:这是底部的第三行:

  

573S1 3 83.03