0

I'm reading in a file as a command line argument. The file contains various information that I want to store in different arrays. I'm confused on how to extract the information from the file so I can add it to the arrays. Here's my code so far:

public class GradeBookApp {

  public static void main(String[] args) throws IOException {

  String name = "";
  char[] categoryCodes = new char[5];
  String[] categories = new String[5];
  double[] categoryWeights = new double[5];
  double[][] gradeTable;
  GradeBook myGB = new GradeBook (name, categoryCodes, 
     categories, categoryWeights);


  if (args.length > 0) {

     for (int i = 0; i < args.length; i++) {
        System.out.println("File \"" + args[i] 
           + "\" read in and Gradebook object created.");
}        

I guess my question is how do I extract the information from the file so I can add it to the arrays.

Here's the file being read in. The first line is the name, the second is the amount of grade categories, the next 5 are the categories and grade weights, and the last ones are the various grades starting with the letter of the category they belong in. These are the values that make up the 2D grade table.

Student1
5
a Activities 0.05
q Quizzes 0.10
p Projects 0.25
e Exams 0.30
f Final 0.30
a100 a95 a100 a100 a100
q90 q80 q100 q80 q80 r90
p100 p95 p100 p85 p100
e77.5 e88
f92

me123
  • 43
  • 6

1 Answers1

0

It's a little difficult to know without knowing the exact format of your file, but I'm presuming it is some form of CVS file.

First step is to load/read the file. Easiest way is to use Apache Common's FileUtils readLines or something similar.

Once you have each line in a String, you can loop over the list and extract the necessary information and add them to your arrays.

If you want to use FileReader, you can do it as:

FileReader reader = new FileReader( args[i] )

where args[i] is the filename and then use the reader as you usually would.

Eric B.
  • 20,257
  • 43
  • 147
  • 269
  • Is there another way instead of using the Apache? I would like to do it without importing anything. – me123 Oct 22 '14 at 01:40
  • Have you even searched at all how to read a text file? http://stackoverflow.com/q/4716503/827480 – Eric B. Oct 22 '14 at 01:43
  • I've always used FileReader, but I'm not sure how to use that when the file is being read in from the command line. – me123 Oct 22 '14 at 01:46