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中的内容,但每个新人都在一行上

亚当·亚当森,adam0001,85.4,79.8,82.4,86.1
Bethany Bright,brig0001,89.7,85.6,84.2,82.9
卡梅隆·卡尔森(carer0001),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
哈里特·霍普,希望0001,52.2,58.93,61.5,63.44
伊万·靛蓝,印度0001,88.4,91.23,90.05,92.46
杰西卡·琼斯(Jessica Jones),jone0001,82.33,89.74,81.3,84.85

最佳答案

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,

关于java - java中的java.util.InputMismatchException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19076757/

10-12 05:24