读取数据文件时出现的数字错误或错误。

huangapple go评论76阅读模式
英文:

Wrong numbers or errors when reading rows of numbers from a data file

问题

以下是翻译好的内容:

public class Diver {
    // ...(省略了类的其余部分)

    public double[] diveScore(int diveNum, double[] scores, double difficulty) {
        // ...(计算得分的部分)
        return scoreArray;
    }
    // ...(省略了其余方法和成员变量)
}

import java.io.File;
import java.io.FileNotFoundException;
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;
    }
    // ...(省略了其余方法)
}

(请注意,由于篇幅限制,以上代码片段可能已被截断。如果需要完整代码,请在评论中指出,我将尽力提供。)

英文:

I'm trying to read a data file and take the 3 rows of numbers and store them into an array. I'm so confused on how I should approach this. Im not even sure if my while loop is even correct. The format of the file is throwing me off. I create variables to hold the name, school, scores, and difficulty for the diver but when I put it in my diveScore method the numbers are wrong, or theres an error reading the file when i don't use the Double.parseDouble() way. The parameters for the method are the dive number (1 to 3), the array of 7 scores, and the difficulty of the dive (1 to 4). Do I also call the diveScore method 3 times for each dive? Each diver has 3 dives which is why there are 3 rows of numbers.

Diver Class:

    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;
}
}

Main File:

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]);
}
}
}

}

Data File:

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
LiPing Sun
Rutgers University
6.5 5 9.5 7.5 8 8.5 7 2.3
7 9 7.5 9 8.5 6.5 8 2.6
6 4 5 9 7 8 6 3.0
Greg McEntire
University of Southern California
9 8 8.5 9.5 9 9.5 7 2.3
9 7 8 8 7.5 8.5 7.5 2.6
8 8 8 8 8 8 8 3.4
Shiva Vadaparthy
Northwestern University
9 8 9.5 9.5 7.5 8 8.5 3.0
8 9 8.5 7.5 9 8.5 8 3.6
7.5 9 8 7 8.5 8 7 4.1
Elizabeth Bennet
Oxford University
7 8 9 8 7 7 8 2.4
8 8 6 7 9 7 8 2.7
8 7 8 6 9 8 9 3.0
Sofia Berrios
Universidad Simon Bolivar
8.5 7.5 9.5 9 7 8.5 8 2.4
8 7 8 9 8 6 7 3.0
7 5.5 6.5 7 5 6.5 7 3.6
Amir Majid
Reed College
4 5 6 5 3 5 7 2.8
7 6 7 8 7 9 8 2.9
8 9 7 9 8.5 8 9 3.4
James Pajuio
University of Pennsylvania
8 7 8 9 8 6 7 2.8
7.5 9 8 7 8.5 8 7 3.0
7.5 9 8 7 8.5 8 7 4.3
Sharon Lewis
Howard University
8 9 9.5 9 8.5 8 9 2.7
4 5 5.5 7 6.5 4.5 5 3.2
8 7 8 9 8 8 7 4.0
Divya Patel
Temple University
7 9 7.5 9 8.5 6.5 8 2.1
8 7 8 6 9 8 9 2.3
3.5 5.5 4 5 3 4 4.5 4.3
Guo Zhang
Portland State University
5 6 5.5 7 6.5 7 5.5 6 3.0
8 7 8.5 9 7.5 8.5 8 3.2
9 8 9.5 8.5 9 8.5 9.5 4.1
Tomasz Wojaczyk
Tufts University
8 8 7 8 7 8 7 2.6
5 4 5 6 7 5 5 2.9
8 6 6 7 8 7 6 3.6

答案1

得分: 1

这里存在一些问题。引发异常的是:

你使用Scannernext()方法读取了一个数字,这会在行尾留下换行符,然后你尝试使用nextLine()方法读取下一个名字,由于扫描器定位在行尾,这会导致返回一个空字符串作为名字。

更严重的是,名字和学校的行后面跟着三行,每行有八个数字,总共二十四个数字,但你只尝试读取了八个数字。

结果是,你得到了"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,最后得到了空的名字和"LiPing Sun",然后是"Rutgers"和"University"这些"数字"…

嗯,你只能走到这里,因为"Rutgers"不是一个数字,当你尝试使用parseDouble将其转换为数字时,程序会崩溃。

另外,你根本没有使用Diver类;你从未创建过一个Diver对象。另外,将数字读入s1等变量,然后解析并放入数组中是没有意义的;你完全可以直接将它们解析到数组中,不需要使用s1,但这只是小问题。

调用kb.close()应该放在finally块中,或者你可以使用try-with-resources块来确保文件被关闭。

我强烈建议阅读Eric Lippert的优秀文章,How to debug small programs;通过其中提供的技巧,你可能根本不需要问这个问题。

英文:

There are a few problems here. The ones that are causing your exception are:

You read a number with the next() method of Scanner, which leaves the end-of-line character at the end of the line, and then you try to read the next name with the nextLine() method, which is going to return an empty string for the name since the scanner is looking at the end of the line.

This is compounded by the fact that the lines for the name and school are followed by three lines of eight numbers each, or twenty-four numbers total, but you only try to read eight numbers.

The result is that you get "Lucieta Spinelli" and "Bryn Mawr College" and then the numbers 8, 9, 8, 7, 8, 8, 8, 2.5 for the first entry, and then the name "" and the school "7 8 7 6 7 7 6 3.7" and then the numbers 8, 7, 6, 7, 7, 6, 8, 3.2 for the second entry, and finally you get the name "" and the school "LiPing Sun" and then the numbers "Rutgers" and "University" and...

Well, that's as far as you get, since "Rutgers" isn't a number and when you try to convert it into one with parseDouble your program crashes.

You also aren't using the Diver class at all; you never create a Diver. There's also no point in reading the numbers into s1 and so on just to parse them and stick them in an array; you might as well just parse them into the array directly and leave s1 out of it, but that's minor.

The call to kb.close() should be inside a finally block, or else you should use a try-with-resources block to ensure the file gets closed.

I would strongly suggest reading Eric Lippert's excellent article, How to debug small programs; with the skills it would provide you with, you would likely not have needed to ask this question at all.

huangapple
  • 本文由 发表于 2020年9月22日 08:52:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/64001664.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定