java中的java.util.InputMismatchException

时间:2013-09-29 10:07:45

标签: java

import java.util.*;
import java.lang.*;
import java.io.*;

public class MinorAssignment_PartB {

public static void main(String[] args) throws Exception {


    List<StudentMarks> marks = new ArrayList<StudentMarks>();
    String File = "studentinfo.txt";
    Scanner scan = new Scanner(new File(File));
    scan.useDelimiter("[,|\\n]");

    while(scan.hasNext()){

    //the error refers to this part here

    marks.add(new StudentMarks(scan.next(), scan.next(), scan.nextDouble(),   scan.nextDouble(), scan.nextDouble(), scan.nextDouble()));

    System.out.printf("%-15s %-15s %-15s %-15s %-15s %-15s %-15s %-15s %n", "Student Name", "Student Fan", "Part A", "Part B", "Part C", "Part D", "Mark", "Grade");

        for (int i = 0; i < marks.size()-1; i++) {
            System.out.println(marks.get(i));

        }
    }


  }
}

我不知道如何解决它,它是在循环和从2个字符串的文本文件中读取然后用逗号分隔的4个双精度,并且有10行循环。

非常感谢任何帮助。

这就是studentinfo.txt中的内容,但每个新人都在一行

Adam Adamson,adam0001,85.4,79.8,82.4,86.1 Bethany Bright,brig0001,89.7,85.6,84.2,82.9 卡梅伦卡尔森,carl0001,55.45,49.82,60.4,42.27 David Dawson,daws0001,72.6,78.49,80.2,65.88 Evelyn Ellis,elli0001,50.2,35.88,48.41,58.37 Frances Fitz,fitz0001,78.9,75.67,82.48,79.1 Greg Gregson,greg0001,24.3,32.88,29.72,28.4 Harriett Hope,hope0001,52.2,58.93,61.5,63.44 Ivan Indigo,indi0001,88.4,91.23,90.05,92.46 Jessica Jones,jone0001,82.33,89.74,81.3,84.85

2 个答案:

答案 0 :(得分:0)

Scanner使用分隔符模式将其输入分解为标记,分隔符模式默认匹配空格。如果使用分隔符,则scan.hasNext()仅适用于该分隔符。要解决您的问题,请在txt文件行的末尾使用逗号,。并使用以下分隔符。

scan.useDelimiter(",");

studentinfo.txt文件应为

Adam Adamson,adam0001,85.4,79.8,82.4,86.1,
Bethany Bright,brig0001,89.7,85.6,84.2,82.9,
Cameron Carlson,carl0001,55.45,49.82,60.4,42.27,
David Dawson,daws0001,72.6,78.49,80.2,65.88,
Evelyn Ellis,elli0001,50.2,35.88,48.41,58.37,
Frances Fitz,fitz0001,78.9,75.67,82.48,79.1,
Greg Gregson,greg0001,24.3,32.88,29.72,28.4,
Harriett Hope,hope0001,52.2,58.93,61.5,63.44,
Ivan Indigo,indi0001,88.4,91.23,90.05,92.46,
Jessica Jones,jone0001,82.33,89.74,81.3,84.85,

答案 1 :(得分:0)

也许你的行结尾很重要(看看分隔符):

    // works for \n line ending
    Scanner scan = new Scanner("A,B,0,0,0,0\nC,D,1,1,1,1\n");
    scan.useDelimiter("[,|\\n]");
    while (scan.hasNext()) {
        System.out.print(scan.next());
        System.out.print(scan.next());
        System.out.print(scan.nextDouble());
        System.out.print(scan.nextDouble());
        System.out.print(scan.nextDouble());
        System.out.print(scan.nextDouble());
        System.out.println();
    }
    scan.close();

    // works for both \r\n and \n line endings
    scan = new Scanner("A,B,0,0,0,0\nC,D,1,1,1,1\r\n");
    scan.useDelimiter(",|\\r?\\n");
    while (scan.hasNext()) {
        System.out.print(scan.next());
        System.out.print(scan.next());
        System.out.print(scan.nextDouble());
        System.out.print(scan.nextDouble());
        System.out.print(scan.nextDouble());
        System.out.print(scan.nextDouble());
        System.out.println();
    }
    scan.close();