组织导入的文本文件,字符串或字符串?

时间:2016-02-08 07:44:16

标签: file text import bufferedreader organization

我有一个文本文件,如下所示:

3333(等)是学生的身份证号码,之后的三个号码是他/她的考试成绩。前两个测试分数为0.3分,第三分为0.4分。零只是分开类。

我有一个缓冲的阅读器来输入所有数据,但我不知道下一步该做什么。转换为字符数组?或者我将每个#保存为int并使用数学函数。

我现在所拥有的只是阅读文件。

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Jose_courseData {
public static void main(String[]args) {
    try (BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\Jose\\Documents\\Loops\\courseData.txt")))
    {

        String sCurrentLine;

        while ((sCurrentLine = br.readLine()) != null) {
            System.out.println(sCurrentLine);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } 

}

文本文件看起来像这样

    0.30 0.30 0.40
    161
    3333 70 60 50
    4444 50 50 50
    5555 80 90 80
    0
    162
    1212 90 85 92
    6666 60 80 90
    7777 90 90 90
    8888 95 87 93
    9999 75 77 73
    0
    263
    2222 90 65 75
    8989 60 40 60
    9090 70 80 30
    0

我需要拍摄文字,整理文章并平均分数。这里应该是什么样的

Grade Data For Class 161
ID Programs Midterm Final Weighted Average Programs grade
-- -------- ------- ----- ---------------- --------------
3333 70 60 50 59.00 Pass
4444 50 50 50 50.00 Fail
5555 80 90 80 83.00 Pass
Class Average: 64.00

Grade Data For Class 162
ID Programs Midterm Final Weighted Average Programs grade
-- -------- ------- ----- ---------------- --------------
1212 90 85 92 ... Pass
6666 60 80 90 ... Fail
7777 90 90 90 ... Pass
8888 95 87 93 ... Pass
9999 75 77 73 ... Pass
Class Average: ...

Grade Data For Class 263
ID Programs Midterm Final Weighted Average Programs grade
-- -------- ------- ----- ---------------- --------------
2222 . . .
8989 . . .
9090 . . .
Class Average: ...

1 个答案:

答案 0 :(得分:0)

您可以使用java.nio获取文件的内容作为String列表,以便您可以按1过滤行.List类有一个toArray(T [] a)方法,您可以使用它来转换它变成了一个字符串数组。然后,您可以通过这些线过滤以确定您将使用哪一个,以及如何打印它。例如:

package Testers;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

public class TestClassResults{

    public static int testone = 30; //Percentages. Marked as integers. These can also be changed
    public static int testtwo = 30;
    public static int testthree = 40;
    public static List<Float> average;
    public static int students;

    public static String[] getAsArray(List<String>ls ){
        return ls.toArray(new String[]{});
    }

    public static void main(String[] args){
    File file = new File("C:\\Users\\Jose\\Documents\\Loops\\courseData.txt");
    Path path = Paths.get(file.toURI());
    try {
        List<String> ls = Files.readAllLines(path);
        String[] array = getAsArray(ls);
        parseClassRecord(array[1]);
        parseStudentRecord(array[2]);
        parseStudentRecord(array[3]);
        parseClassAverage();
        //You can keep parsing class and student records to match whatever you need to do
    } catch (IOException e) {
        e.printStackTrace();
    }
    }
    public static void parseClassRecord(String classno){
        System.out.println("Grade Data for Class " + classno);
        System.out.println("ID Programs Midterm Final Weighted Average Programs grade");
        System.out.println("-- -------- ------- ----- ---------------- --------------");
    }

    public static void parseStudentRecord(String studentfacts){
        System.out.print(studentfacts + " ");
        String first = studentfacts.substring(6, 7);
        String second = studentfacts.substring(9, 10);
        String third = studentfacts.substring(12, 13);
        int one = Integer.parseInt(first);
        int two = Integer.parseInt(second);
        int three = Integer.parseInt(third);
        float a = one/testone;
        float b = two/testtwo;
        float c = three/testthree;
        float percent = (a + b + c) /3;
        System.out.print(percent + " ");
        if(percent > 0.5){ //If the student scored more than 50% in a test
            System.out.print("Pass");
        }else{ // If they didn't
            System.out.print("Fail");
        }
        System.out.println();
        average.add(Float.valueOf(percent));
        students = students + 1; // Also can use students++ (it does the same thing).
    }

    public static void parseClassAverage(){
        float total = 0;
        for(int i = 0; i < average.size(); i++){
            total = total + average.get(i);
        }
        float avg = total / students;
        System.out.println("Class Average: " + avg);
        System.out.println(); // Creates a space between the lines.
    }

}

我注意到,我的方法不使用BufferedReader,因为我发现读取单行很简单,但对于多行文件,我发现将它们作为列表更容易,因为List和Collection API有特殊的方法。