0

hello this is my first time posting, please let me know if i am doing anything wrong in the question-asking process.

my purpose: to save double variables in a csv file and to save them in an arraylist in java file.

i so far have the current code, i am a beginner in java so please dont flame ToT

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

public class CsvProcessor {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);

        // ask for file path at command prompt, save the path input in path
        System.out.print("path of file: ");
        String path = keyboard.next();

        try {
            // set up file, reader, and arraylist for processing csv file
            java.io.File csv = new java.io.File(path);
            BufferedReader bufferedReader = new BufferedReader(new FileReader(csv));
            ArrayList<String> csvEntry = new ArrayList<String>();

            // set parameters for StringTokenizer
            String line = "";
            String delimiter = ",";

            //read using bufferedReader and save using csvEntry ArrayList
            while ((line = bufferedReader.readLine()) != null) {    
                StringTokenizer stringTokenizer = new StringTokenizer(line, delimiter);
                while (stringTokenizer.hasMoreTokens()) {
                    csvEntry.add(stringTokenizer.nextToken());
                }
            }

            csvEntry.toArray();
            double[] csvDouble = new double[csvEntry.getLength()];
            for (int i = 0; i < csvEntry.getLength(); i++) {
                csvDouble[i] = Double.parseDouble(csvEntry[i]);
            }

            int x = csvDouble.getLength();
            double sum = 0;

            for (int y = 0; y < x; y++) {
                sum = sum + csvDouble[y];
            }

            double mean = sum / x;
            System.out.println("mean is: " + mean);
            bufferedReader.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

i tried things like converting the strings in the csv file and saving them into the arraylist, then converting the arraylist into an array which is then next converted into a array of double. the error i am getting is:

C:\Users\user\Codes\ktrt>java CsvProcessor
path of file: C:\Users\user\Codes\ktrt\montyreal.csv
Exception in thread "main" java.lang.NumberFormatException: For input string: "5
48.53"
        at java.lang.NumberFormatException.forInputString(Unknown Source)
        at java.lang.Integer.parseInt(Unknown Source)
        at java.lang.Integer.<init>(Unknown Source)
        at CsvProcessor.main(csvprocessor.java:27)

thank you very much for the help.

1 Answers1

0

First of all, you have a wrong error log. Your code doesn't have an Integer.parseInt() method. Let's say that you correctly parse the CSV into csvEntry variable, this is what you can do:

List<Double> csvDouble = new ArrayList<Double>();
double mean, total = 0;

for (String raw : csvEntry) {
  double entry = Double.valueOf(raw);
  total += entry;
  csvDouble.add(entry);
}

mean = total / csvDouble.size();
// at this point you have your ArrayList and mean value;
System.out.println("mean is " + mean);

Hope this helps :)

Iqbal Djulfri
  • 718
  • 6
  • 14
  • what do you mean by having wrong error code? i have double.parsedouble(csventry[i]) to convert the strings saved in csventry to double variables, and then i try to store them into csvdouble, which is an arraylist of doubles. – monty mont Oct 31 '12 at 07:37
  • There's no `Integer.parseInt()` in your code (which stated in your log). You have `Double.parseDouble()` instead. – Iqbal Djulfri Oct 31 '12 at 08:35