0

I need to take a CSV file and find the averages of each row and column. There is 922 rows & 11 columns. So far I have been able to print the values to the console, but they arent separated by rows and columns, just one value and then a new line. So far I have:

public static void main(String[] args) {

    String fileName = "eeg.csv";
    File file = new File(fileName);

    try {
        Scanner inputStream = new Scanner(file);
        while (inputStream.hasNext()){
            String data = inputStream.next();
            System.out.println(data);
        }
        inputStream.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

}

}

I'd appreciate any help to sort the values back into rows & columns, and any advice on how to find the averages by row and column. Thank you.

  • Use a library designed for the job, something like openCSV – MadProgrammer Nov 05 '15 at 01:23
  • 1
    I vote for Apache POI – Michael Queue Nov 05 '15 at 01:26
  • @TheLima openCSV is that big (compared to the apache API) and CSV is more complicated then just values separated by commas, so no need to reinvent the wheel. I think one of the best skills any developer can develop is knowing when a 3rd party library will solve there problem and when spending the time to roll there own is a better solution. And all the parsing you'd have to do by hand can be accomplished in one or two lines of simple code, just saying – MadProgrammer Nov 05 '15 at 01:44
  • Possible duplicate of [CSV parser/reader for C#?](http://stackoverflow.com/questions/906841/csv-parser-reader-for-c) – Steve Nov 05 '15 at 01:56
  • @TheLima And if the column data contains `,`? This is the reason why I'd suggest using a dedicated library, parsing CVS seems simple, but it gets messy quickly. I hope you'd not recommend people to hand parse HTML and XML as well :P – MadProgrammer Nov 05 '15 at 02:04
  • @TheLima I agree with MadProgrammer. When I first started learning java we had an assignment to use a third party library which is when I learned about Apache POI. While, I am not against the argument of reinventing to learn, I gained an invaluable skill by utilizing a third partly library. Additionally, csv can be a pain in the butt. Especially when the user doesn't know the difference between CSV and XLSX. Not to mention workbooks. With Apache manipulating these documents becomes fairly trivial. – Michael Queue Nov 05 '15 at 02:07
  • @TheLima And all I'm saying, rather then wasting the op's time trying to reinvent, what is actually a complex issue, a library would solve the problem and allow them to focus on actually achieving their goal simply and quickly. What looks like a "simple" problem is actually much more complex then it looks. Is the question about parsing the csv or how to load it so they can process it? – MadProgrammer Nov 05 '15 at 19:58
  • @TheLima And there's the problem, we don't know if this a "learning" exercise or not. Has the OP been dumped with a data file they've now been asked to process, in which case a library would be a better solution. We're both making assumptions about the OP's requirements without enough context, I don't disagree with you, I just don't see the point in your saying it's not worth using a library. Both cases are valid, but I'd prefer to teach my devs skills which they can use daily to make their lives easier and turn around the code quicker, but that is a particular use case ;) – MadProgrammer Nov 06 '15 at 01:05
  • @TheLima I don't see anywhere the op has stated that they want/need to do it manually, I do see they have a need to process the values in the file to generate a result, and to there credit, they've made an attempt to start. Beyond condition statements and loops, programming is all about the Apis. My only complaint is your assertion that a library is not required to solve the problem, which is a viable option for the problem, as is doing it manually, but since I don't have enough context, I left it as a comment ;) – MadProgrammer Nov 06 '15 at 01:29
  • @TheLima it wasn't intended to be offensive – MadProgrammer Nov 06 '15 at 03:11
  • @TheLima *"Again, if the only way you know to get your code working is to use APIs, what happens when you face a task for which there are none?"* then you make one, your argument (not saying it's a bad one) seems to assume the using a library (core or otherwise) doesn't require basic programming skill. In the process of teaching myself objective-c, once I got a basic grasp on the language syntax, I've spent most of my time looking at he core/third party libraries – MadProgrammer Nov 06 '15 at 03:16
  • @TheLima At the end of the days, it's about appropriate use of library functionality, you could solve the problem with String parsing, regular expression (core **library**) or a dedicated library which has had thousands of developer hours put into. As I said, I have no issue with your desire to answer the question with a manual approach, I like leading developers to water too, I'm just whinging over the idea that they don't need to use a dedicated library, of which, they'd still need basic programming skills to use. – MadProgrammer Nov 06 '15 at 03:36
  • @TheLima The library just solves the complexity of the getting from point a (raw data) to point b (structured data) from which they can start processing the data, which I saw as been the goal the op, but that's me. Right tool for the job ;) – MadProgrammer Nov 06 '15 at 03:38
  • @MadProgrammer *"then you make one"* - If you know how to, which is my point. *"(...) assumes that using a lib doesn't require basic programming skill"* - If you're talking about the low-level code, then no, it usually doesn't; most libs exist exactly to abstract or eliminate the low-level work. *"The library just solves the complexity (...)"* - Yes, and from my POV, it solves *too much* of it, if you don't understand the underlying process the lib is doing for you. In my POV, the structures and techniques used with libs are different; you usually don't do parsing, for (a convenient) example. – CosmicGiant Nov 06 '15 at 12:01
  • @MadProgrammer Anyways, I think this discussion has already way passed the point of *"extended discussions in comments"*...I'll delete my previous comments, and we can continue on chat if you'd like. – CosmicGiant Nov 06 '15 at 12:04

2 Answers2

0

For most cases with CSV, it's enough to (read and) parse the file by lines or groups of lines first, to get the rows; then split each line by separator patterns, which for CSV are \s*,\s*, to get each line's columns.

Traversing the rows and columns to find the average after that should be trivial.

More advanced situations, like "quoted blocks" which can contain the separator ,, new lines, or even other quoted values (usually handled as double quotes "" so that the parser can know the difference), would be better resolved using Pattern and Matcher, or other parsing techniques.

CosmicGiant
  • 6,275
  • 5
  • 43
  • 58
0

Use Apache POI library.

public int getPhysicalNumberOfRows();
public int getFirstRowNum();
public int getLastRowNum();
Thirumal
  • 8,280
  • 11
  • 53
  • 103