Ross Vasquez February 2016

Organizing imported text file, char or strings?

I have a text file that reads like this:

The 3333(etc.) are a students ID number, and the three numbers after are his/hers test scores. 0.3 weight for the first two test scores, 0.4 weight for the third. The zero is just to separate the classes.

I have a buffered reader to input all the data, but im not sure what to do next. do I convert to a character array? or do I save each # as an int and use math functions.

All I have is this right now just to read the file.

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

}

text file looks like this

    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

I need to take the text, organize it, and average the scores. here's what it should look like

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 P        

Answers


JD9999 February 2016

You can use java.nio to obtain the contents of a file as a String list so that you can filter the lines 1 by 1. The List class has a toArray(T[]a) method which you could use to convert it into a string array. You can then filter through the lines to work out which one you will use, and how to print it. For example:

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

Post Status

Asked in February 2016
Viewed 3,239 times
Voted 12
Answered 1 times

Search




Leave an answer