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.