1
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 Cameron Carlson,carl0001,55.45,49.82,60.4,42.27 David Dawson,daws0001,72.6,78.49,80.2,65. ,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,603. ,88.4,91.23,90.05,92.46 杰西卡·琼斯,jone0001,82.33,89.74,81.3,84.85

4

2 回答 2

0

AScanner使用分隔符模式将其输入分解为标记,默认情况下匹配空格。如果您使用分隔符,则 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,
于 2013-09-29T10:22:05.207 回答
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();
于 2013-09-29T10:56:32.197 回答