从数据文件中读取数字行时数字错误或错误

问题描述

我正在尝试读取数据文件,并提取3行数字并将其存储到数组中。我对如何处理这个问题感到很困惑。我什至不确定我的while循环是否正确。该文件的格式让我失望。我创建了变量来保存潜水员的姓名,学校,分数和难度,但是当我将其放入divescore方法中时,数字是错误的,或者当我不使用{时,会出现读取文件错误{1}}方式。该方法的参数包括潜水编号(1至3),7个分数的数组以及潜水难度(1至4)。每次潜水时,我是否还会调用一次divescore方法3次?每个潜水员都有3次潜水,这就是为什么要列出3行数字的原因。

潜水课程:

Double.parseDouble()

文件

    public class diver {
    
    private String name;
    private String school;
    private double [] scoreArray = new double[3];
    private double totalscore;

    diver() {
        
    }
    diver(String name,String school) {
        this.name = name;
        this.school = school;
    }
    //loop through score array and calculate the total score for each dive attempt
    public double [] divescore(int diveNum,double [] scores,double difficulty) {
        double min = min(scores);
        double max = max(scores);
        
        for (int i = 0; i < scores.length; i++) {
            totalscore += scores[i];
        }
        totalscore -= max;
        totalscore -= min;
        totalscore *= difficulty;
        
        for (int i = 0; i < scoreArray.length; i++) {
            scoreArray[i] = totalscore;
        }
        return scoreArray;
    }
    //finds smallest score in array of scores
    private double min(double [] scores) {
        java.util.Arrays.parallelSort(scores);
        
        double min = scores[0];
        return min;
    }
    //finds largest score in array of scores
    private double max(double [] scores) {
        java.util.Arrays.parallelSort(scores);
        
        double max = scores[scores.length - 1];
        return max;
    }
    //calculates total of the 3 dives
    public double totalscore() {
        for (int i = 0; i < scoreArray.length; i++) {
                totalscore += scoreArray[i];
        }
        return totalscore;
    }
    public String toString() {
        String str = name + "," + school + ": " + totalscore + "\n" + "dive 1: " + scoreArray[0] + "\n" + "dive 2: " + scoreArray[1] + "\n" + "dive 3: " + scoreArray[2] + "\n";
        return str;
    }
}

}

数据文件

import java.io.File;
import java.io.FileNotFoundException;
import java.util.InputMismatchException;
import java.util.Scanner;

public class Main {

public static void main(String[] args) throws FileNotFoundException {
    diver [] divers = new diver[25];
    int numdivers = readFile(divers);
    System.out.println(numdivers);
    
}
public static int readFile(diver [] divers) throws FileNotFoundException {
    File f = new File("divers.dat");
    Scanner kb = new Scanner(f);
    diver d;
    
    int count = 1;
    int diveNum = 1;
    while (kb.hasNextLine()) {
        String name = kb.nextLine();
        String school = kb.nextLine();
        String s1 = kb.next();
        String s2 = kb.next();
        String s3 = kb.next();
        String s4 = kb.next();
        String s5 = kb.next();
        String s6 = kb.next();
        String s7 = kb.next();
        String diff = kb.next();
        
        
        double score1 = Double.parseDouble(s1);
        double score2 = Double.parseDouble(s2);
        double score3 = Double.parseDouble(s3);
        double score4 = Double.parseDouble(s4);
        double score5 = Double.parseDouble(s5);
        double score6 = Double.parseDouble(s6);
        double score7 = Double.parseDouble(s7);
        double difficulty = Double.parseDouble(diff);
        
        double [] scores = new double [7];
        scores[0] = score1;
        scores[1] = score2;
        scores[2] = score3;
        scores[3] = score4;
        scores[4] = score5;
        scores[5] = score6;
        scores[6] = score7;
            
        count++;
        diveNum++;
    }
    kb.close();
    return count;
}
public static void printdivers(diver [] divers,int numdivers) {
    System.out.println("All divers\n");
    for (int i = 0; i < divers.length; i++) {
        if (divers[i] != null) {
            System.out.println(divers[i]);
        }
    }
}

解决方法

这里有一些问题。引起您异常的原因是:

您使用next()的{​​{1}}方法读取了一个数字,该方法将行尾字符留在行尾,然后尝试使用{ {1}}方法,由于扫描程序在该行的末尾,因此该方法将返回一个空字符串作为名称。

由于名称和学校的行后面紧跟着三行,每行八个数字,或总共二十四个数字,这又使事实更加复杂,但是您只能读取八个数字。

结果是您获得“ Lucieta Spinelli”和“ Bryn Mawr College”,然后是第一个条目的数字8、9、8、7、8、8、8、2.5,然后是名称“”和学校“ 7 8 7 6 7 7 6 3.7”,然后输入数字8、7、6、7、7、6、8、3.2作为第二个条目,最后您获得名称“”和学校“孙丽萍” ”,然后是数字“ Rutgers”和“ University”,以及...

好吧,就您所能掌握的而言,因为“罗格斯”不是数字,并且当您尝试使用Scanner将其转换为数字时,您的程序会崩溃。

您也根本没有使用nextLine()类;您永远不会创建潜水员。将数字读入parseDouble也是没有意义的,以此类推只是解析它们并将它们粘贴在数组中。您也可以直接将它们解析到数组中,然后将Diver保留在数组中,但这是次要的。

s1的调用应该在s1块内,否则您应该使用try-with-resources block来确保文件被关闭。

我强烈建议您阅读埃里克·利珀特(Eric Lippert)的出色文章How to debug small programs;凭借它可以为您提供的技能,您可能根本不需要问这个问题。