0

So I want to create a constructor that reads in a line of a file from a csv and save the first token into a variable and the remaining tokens into an array. This constructor will be used in a gradebook application but being new to txt/file manipulation I'm having a hard time.

A line will look like:

Billy Bob,68,79,95,83

I want to separate the tokens into these:

name = Billy Bob

grades[] = "68,79,95,83"

here is the code I have so far:

import java.io.*;


public class gradeBook {

    public static void main(String[] args){

        System.out.println("Java Grade Book version 1.0");

        int lineCounter = 0;
        String array[];

        try{
            File data = new   File("/file/path/that/works");
            InputStream f = new FileInputStream(data);
            BufferedReader br = new BufferedReader(new   InputStreamReader(f));

            for (String line = br.readLine(); line != null; line =     br.readLine()) {
                System.out.println(line); // just here to check that the code is working thus far

                //insert code here
                //name should equal first token (which is two names like Billy Bob)
                //grades[] should contain the other double type tokens (e.g. 56,87,89,90)
            }

            br.close();
        }
        catch(Exception e){
            System.err.println("Error: File Couldn't Be Read");
        }
    }
}

And I want to loop through the file to get as many students as are on the file stored so I can manipulate the grades for averages among other things. This is a personal project to help improve my developing skills so any help, useful tutorial links, and tips will be greatly appreciated. But please don't suggest simplistic examples like the many tutorials I have already read that only use one data type.

Thanks for any help!

4
  • have you implemented reading the file yet? Commented Nov 30, 2016 at 17:56
  • buffer reader and some other random things I don't really understand that I found in tutorials that didn't help. I can not find a definitive way to tokenize a line and store the tokens into variables, arrays, etc. Commented Nov 30, 2016 at 17:57
  • 1
    I know this Question is for practice, but FYI in real work I would use the Apache Commons CSV library to process such files. Commented Nov 30, 2016 at 20:02
  • Good to know thanks, is there a tutorial page or book you could recommend for learning more about Apache Commons? Commented Nov 30, 2016 at 20:11

2 Answers 2

1

Split the line into an array;

String[] input = line.split(",");

String variable = input[0];

int[] grades= new int[input.lenght - 2];

for(int i = 1; i < input.length; i++)
{
    grades[i] = input[i];// you might have to do Integer.pareseInt(input[i]);
}

I did not write this in an IDE, but the logic should be correct. You are going to run into a new problem. You grade book will only contain the last entry. Try using a 2D array for grades and 1D array for names; I personally would not use arrays. I would use arraylist.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! I will give it a shot. Why use array list over array for this program?
I would only use arrays if I know the number of items that will be read from the file and this number never changes. Arraylist grows as needed.
Good point, I'll be sure to implement array lists when this program grows.
1

So I haven't tested computing my tokens with methods or anything else yet but I have tokenized the line to sum (ha ha oops, meant some) degree with this bit of code:

String[] tokens = line.split(",");
            String name = tokens[0];
            String grade1 = tokens[1];
            String grade2 = tokens[2];
            String grade3 = tokens[3];
            String grade4 = tokens[4];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.