I am new to Java and I am reading a csv file using open CSV and my code is below:
import java.io.FileReader; import java.util.Arrays; import au.com.bytecode.opencsv.CSVReader; public class ParseCSVLineByLine { double arr []=new arr[10]; public static void main(String[] args) throws Exception { //Build reader instance //Read data.csv //Default seperator is comma //Default quote character is double quote //Start reading from line number 2 (line numbers start from zero) CSVReader reader = new CSVReader(new FileReader("data.csv"), ',' , '"' , 1); //Read CSV line by line and use the string array as you want String[] nextLine; int i=0; while ((nextLine = reader.readNext()) != null) { if (nextLine != null && i<10) { //Verifying the read data here arr[i]=Double.parseDouble(Arrays.toString(nextLine).toString()); System.out.println(arr[i]); } i++; } } } But this does not works.But when I only print
Arrays.toString(nextLine).toString() This prints
[1] [2] [3] . . . . [10] I think the conversion is having the problem.Any help is appreciated.
Arrays.toString(nextLine)will most likely result in something that isn't parseable as a double, since even with only one element you'll get the strings you posted, i.e."[1]". Why not justDouble.parseDouble(nextLine[index])whereindexis probably 0?