Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中从一个数组中提取项,然后放入另一个数组,并计算总数_Java - Fatal编程技术网

如何在Java中从一个数组中提取项,然后放入另一个数组,并计算总数

如何在Java中从一个数组中提取项,然后放入另一个数组,并计算总数,java,Java,首先,我这样做是为了我的一项任务,我不希望被填鸭式的喂养。只要在正确的方向上提供一些指导就好了 我需要编写一个java应用程序,打开用户输入的文本文件,该文本文件包含以下数据 Grades.txt 57363 Kuveshan D D C P H H C D 72992 Jason Green H H H D D H H H 71258 Mistey Norms C F C D C C C P 70541 Ben Dover F F F P C C C F 46485 Justin Time F

首先,我这样做是为了我的一项任务,我不希望被填鸭式的喂养。只要在正确的方向上提供一些指导就好了

我需要编写一个java应用程序,打开用户输入的文本文件,该文本文件包含以下数据

Grades.txt

57363 Kuveshan D D C P H H C D
72992 Jason Green H H H D D H H H
71258 Mistey Norms C F C D C C C P
70541 Ben Dover F F F P C C C F
46485 Justin Time F C F C D P D H
61391 Anna Conda D D F D D F D D
88985 Bob Down P F P F P F P P
上面是:学生ID,姓名,然后是8个字母:F,C,D,p,H这些是分数,即不及格,学分,优异,及格,优异

下面是我到目前为止编写的代码,它打开文本文件,并将文本文件中的每一行放入一个数组项中

    package accountFilesDemo_17259747;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

/**
 * This below is an the full path used for user to enter
 * path to grades
 * C:\\Users\\Vick\\PT\\accountFilesDemo_17259747\\src\\accountFilesDemo_17259747\\Grades.txt
 * yours (the markers) will be different
 * @author vkumar
 *
 */


public class StudentGPA_17259747 {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter file name: ");

        // Get the file name from the user
        String filename = keyboard.nextLine();
        String[] array = new String[100];
        try (BufferedReader br = new BufferedReader(new FileReader(filename)))
        {

            String sCurrentLine;
            int i = 0;

            while ((sCurrentLine = br.readLine()) != null) {
                System.out.println(sCurrentLine);
                array[i] = sCurrentLine;
                i++;
            }

        } catch (IOException e) {
            e.printStackTrace();
        } 

        // studentID array
        String[] studentID = new String[array.length];

        // student name array
        String[] studentName = new String[10];

        // student scores array
        String[] studentScores = new String[50];


        for(int i = 0; i < array.length; i++)
        {
            studentID[i] = array[i].substring(0, 5);System.out.println(studentID[i]);
        }

        //System.out.println(array[2]);
        // prints: 71258 Eileen Over C F C D C C C P
        //String[] studentID = new String[array.length];
        //for(int i = 0; i < array.length; i++)
        //{
        //  studentID[i] = array[i].substring(0, 5);
        //  System.out.println(studentID[i]);
        //s}


    }

}
我什么都不需要名字,我想知道如何获得这8个字母,并将上面的数字分配给他们。打开文本文件并将其读入数组后,我需要获取studentID和8个字母,然后使用这8个字母获取它们的数值,将它们相加,执行GPA公式等,并使用以下内容保存文本文件:

学生成绩 29292 3.4 ^ 例如

解决方案受到赞赏,反馈受到赞赏,负面评论也受到赞赏,但总体而言,比起好的例子、理论、阅读资料等来源,更喜欢指导


谢谢。

据我所知,您将字母存储在字符串列表中。你只需要把它们转换成数字 我建议对此使用枚举。还可以将字符串列表转换为枚举

尝试使用此枚举作为引用

public enum Grade {
    H(7), D(6), C(5), P(4), F(0);

    private int value;

    private Grade(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }

    public static Grade getGrade(char ch) {
        switch (ch) {
        case 'H':
            return H;
        case 'D':
            return D;
        case 'C':
            return C;
        case 'P':
            return P;
        case 'F':
            return F;
        default:
            return null;
        }
    }
}

我们将向您解释一行包含的内容。这是分组数据或对象的逻辑单元。重新开始,记住这一点。字符串[]不是正确的数据结构,这就是您遇到问题的原因。我建议使用String.split方法,最好使用HashMap来存储成绩。每个成绩对于每个学生都是不同的数字吗?成绩基于GPA,即1个学生获得F C C,即不及格、学分、学分,另一个只得到F,它是0+0+0+0=0,我尝试了string.split,但没有找到将它们拆分为可用变量的方法,Spider什么意思?它不是正确的数据结构?建议使用HashMap键和值对以各自的字母存储等级。
public enum Grade {
    H(7), D(6), C(5), P(4), F(0);

    private int value;

    private Grade(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }

    public static Grade getGrade(char ch) {
        switch (ch) {
        case 'H':
            return H;
        case 'D':
            return D;
        case 'C':
            return C;
        case 'P':
            return P;
        case 'F':
            return F;
        default:
            return null;
        }
    }
}