0

So basically what I need to do is:

Read a text file like this:

[Student ID], [Student Name], Asg 1, 10, Asg 2, 10, Midterm, 40, Final, 40
01234567, Timture Choi, 99.5, 97, 100.0, 99.0
02345678, Elaine Tam, 89.5, 88.5, 99.0, 100

and present it like this (with calculations of rank and average):

ID Name Asg 1 Asg 2 Midterm Final Overall Rank

01234567 Timture Choi 99.5 97.0 100.0 99.0 99.3 1

02345678 
Elaine Tam 89.5 88.5 99.0 100.0 97.4 2

 Average: 94.5 92.75 99.5 99.5 98.3

Using printf() function

now this is what I have done so far:

import java.io.*;
import java.util.Scanner;

class AssignmentGrades {
    public static void main(String args[]) throws Exception {
        Scanner filename = new Scanner(System.in);
        String fn = filename.nextLine(); //scannig the file name
        System.out.println("Enter your name of file : ");

        FileReader fr = new FileReader(fn+".txt");

        BufferedReader br =  new BufferedReader (fr);
        String list;

        while((list = br.readLine()) !=null) {
            System.out.println(list);
        }
        fr.close();
    }
}

So I can ask the user for the name of the file, then read it and print.

Now.. I'm stuck. I think I need to probably put it in to array and split?

String firstrow = br.readLine();
String[] firstrow = firstrow.split(", ");

something like that?.. ugh ive been stuck here for more than an hour

I really need help!! I appreciate your attention!! ( I started to learn java this week)

SH Park
  • 1
  • 2
  • `Scanner` can be used to read integers etc based on some separator (here `, `). see [the javadoc](http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html) –  Jan 10 '15 at 09:20

2 Answers2

0

There are two ways for splitting the input line just read from the file

  1. Using String object's split() method which would return an array. Read more about the split here.

  2. StringTokenizer Class - This class can be used to divide the input string into separate tokens based on a set of delimeter. Here is a good tutorial to get started.

You should be able to get more examples using google :)

In case you want to parse integers from String. Check this.

Community
  • 1
  • 1
Keen Sage
  • 1,899
  • 5
  • 26
  • 44
0

Here I store the columns as an array of Strings and I store the record set as an ArrayList of String arrays. In the while loop if the column set is not initialized yet (first iteration) I initialize it with the split. Otherwise I add the split to the ArrayList. Import java.util.ArrayList.

    String[] columns = null;
    ArrayList<String[]> values = new ArrayList<String[]>();
    String list;

    while((list = br.readLine()) !=null) {
        if (columns != null) {
            columns = list.split(", ");
        } else {
            values.add(list.split(", "));
        }
    }
    fr.close();
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175