-1

I have a .csv file and I understand how to read it in Java, my problem is I want to be able to put the values of what is inside the file into a string and then read the string so I can calculate these values.

Alice Jones,80,90,100,95,75,85,90,100,90,92 Bob
Manfred,98,89,87,89,9,98,7,89,98,78

Those are the values, now how could I add up all the integers next to each name and create an average? MY biggest problem was when I read the file it was in a While loop and then was able to print out the lines just as it looks above into the console but when I wanted to print the values outside of the While loop it said the string doesn't exist so therefore I can't calculate the values if there is nothing in it.

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

 public class Grades {
public static void main(String args[]) throws IOException
 {
 try{
 // Open the file that is the first 
 // command line parameter
 FileInputStream fstream = new FileInputStream("filescores.csv");


 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
 String strLine;
 //Read File Line By Line
 while ((strLine = br.readLine()) != null)   {
 // Print the content on the console
 String line = strLine
 System.out.println (strLine);
 }
 //Close the input stream
 in.close();
 }catch (Exception e){//Catch exception if any
 System.err.println("Error: " + e.getMessage());

 }
 }
 }
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 3
    Show us that code. Probably your are facing problem with scope of variable. – Smit Apr 24 '13 at 15:30
  • You want to keep a reference to the values you visit while iterating through your `while` loop. You'll then have access to them so you can sum them or whatever. – Sotirios Delimanolis Apr 24 '13 at 15:31
  • Split on commas, iterate over the results (skipping the first), convert to a number, and add them. – Dave Newton Apr 24 '13 at 15:38
  • Please don't use DataInputStream to read text. It is redundant and confusing. Please remove it from your example as this bad code gets copied a lot. – Peter Lawrey Apr 28 '13 at 18:14

2 Answers2

0

I would suggest to implement a plain old java object (POJO) and store the data there:

public class MyRow {
    public final String name;
    public final int[] values;

    public MyRow(String name, int[] values) {
        this.name=name;
        this.values=values;
    }
}

And split your string and put it in a simple List<MyRow>:

MyRow splitData(String line) {
    String[] parts=line.split(" ");
    int[] vals=new int[parts.length];
    // starting with one for skipping the name
    for(int i=1; i<parts.length; i++) {
        vals[i-1]=Integer.parse(parts[i]);
    }
    return new MyRow(parts[0], vals);
}

So basically your read loop will look like this:

List<MyRow> data = new ArrayList<MyRow>();
while((strLine = br.readLine()) != null) {
    // Print the content on the console
    System.out.println (strLine);
    data.add(splitData(strLine));
}

Now all the data are in the data List.

rekire
  • 47,260
  • 30
  • 167
  • 264
0

when I wanted to print the values outside of the While loop it said the string doesn't exist

As I commented you have scope issue in your code. However I dont see anything outside of while loop that your are printing or doing anything. You can use following code snippet to print the line outside of while loop.

ArrayList<String> lines = new ArrayList<String>();// Updated <String>

 while ((strLine = br.readLine()) != null)   {
   // Print the content on the console
   lines.add(strLine);
   System.out.println(strLine);
 } 

for(int i = 0; i < lines.size(); i++){
 System.out.println(lines.get(i));
}

Variable Scope

EDIT for comment

Note: Grades.java uses unchecked or unsafe operations.

Note: recompile with -Xlint: unchecked for details.

The above are just warning given by compiler to make you aware about unsafe collection use. See here for more details.

Community
  • 1
  • 1
Smit
  • 4,685
  • 1
  • 24
  • 28
  • I used the code you gave me and I'm still receiving an error. This is what I got this time. Note: Grades.java uses unchecked or unsafe operations. Note: recompile with -Xlint: unchecked for details. – Tom Manolakis Apr 24 '13 at 15:50
  • @TomManolakis I updated the answer. And that not error, just a warning. [See here](http://stackoverflow.com/questions/197986/what-causes-javac-to-issue-the-uses-unchecked-or-unsafe-operations-warning). You also have to add `import java.util.ArrayList;` above the class. – Smit Apr 24 '13 at 15:59