Parsing different types of data format from a CSV file












-1















I am still a beginner with Java programming so I apologise in advance if I am over-complicating my problem.



What is my program?
I am building a GUI based program. The goal of the program is to load a CSV, XML or JSON file and for the program to then store the data into an Array. The data will then be displayed in a text box. Ultimately, the program will have the ability to plot data to a graph.



GUI Details:




  • 3 Radio buttons - Allows the user to select CSV, XML OR JSON

  • Load File Button

  • Display Button - Displays the data into the textArea

  • Display Graph Button

  • Text Area


Problem: I am having trouble storing the data into an Array. I believe this is because of the format of the data. So for example, this is the first 3 lines of the CSV file:



millis,stamp,datetime,light,temp,vcc
1000, 1273010254, 2010/5/4 21:57:34, 333, 78.32, 3.54
2000, 1273010255, 2010/5/4 21:57:35, 333, 78.32, 3.92
3000, 1273010256, 2010/5/4 21:57:36, 344, 78.32, 3.95


(Note - there are 52789000 lines of data in the CSV/XML/JSON files)



The CSV-Reader Class contains the method for reading through the data, storing it into an array and then storing it to a dataList.



As you can see from the above example, some of the data types are much different. I am having particular trouble with splitting/parsing the time and date variables.



Here is what my CSV-Reader Class code looks like at the moment (Again, I apologise for noob code).



import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class CSVReader {

//create a class that will hold arraylist which will have objects representing all lines of the file

private List<Data> dataList = new ArrayList<Data>();
private String path;

public List<Data> getDataList() {
return dataList;
}

public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}

//Create a method to read through the csv stored in the path
//Create the list of data and store in the dataList

public void readCSV() throws IOException{

//i will create connection with the file, in the path
BufferedReader in = new BufferedReader(new FileReader(path));

String line = null;
line = in.readLine();

while((line = in.readLine())!=null){

//I need to split and store in the temporary variable and create an object


String splits = line.split("\s*(=>|,|\s)\s*");

long millis = Long.parseLong(splits[0].trim());
long stamp = Long.parseLong(splits[1].trim());
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/M/d HH:mm:ss");
System.out.println(splits[2].trim());
LocalDateTime dateTime = LocalDateTime.parse(splits[2].trim(), formatter);
LocalDate dateTime = dateTime.toLocalDate();
LocalTime time = dateTime.toLocalTime();
int light = Integer.parseInt(splits[3].trim());
double temp = Double.parseDouble(splits[4].trim());
double vcc = Double.parseDouble(splits[5].trim());

Data d = new Data(millis,stamp,datetime,light,temp,vcc);//uses constructor


//final job is to add this object 'd' onto the dataList
dataList.add(d);

}//end of while loop

}


Any help would be greatly appreciated!



Edit 1 - I thought that date and time were seperate CSV headers. They were not. Therefore the time variable has been deleted from the program. It has been replaced with the datetime variable.



Edit 2 - My program is now reading the CSV file up until line 15 of the csv




27000, 1273010280, 2010/5/4 21:58:0, 288, 77.74, 3.88




CONSOLE ERRORS



Exception in thread "AWT-EventQueue-0" 
java.time.format.DateTimeParseException: Text **'2010/5/4 21:58:0'** could not
be parsed at index 15
at java.time.format.DateTimeFormatter.parseResolved0(Unknown Source)
at java.time.format.DateTimeFormatter.parse(Unknown Source)
at java.time.LocalDateTime.parse(Unknown Source)
at CSVReader.readCSV(CSVReader.java:55)
at GUI$2.actionPerformed(GUI.java:85)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)









share|improve this question

























  • Why are you trying to parse a date as a double?

    – Joe C
    Nov 25 '18 at 18:05











  • Because I thought that was most suitable...what should it be?

    – rflwill
    Nov 25 '18 at 18:09











  • You may wish to explore the java.time package for appropriate types dealing with dates and times.

    – Joe C
    Nov 25 '18 at 18:18
















-1















I am still a beginner with Java programming so I apologise in advance if I am over-complicating my problem.



What is my program?
I am building a GUI based program. The goal of the program is to load a CSV, XML or JSON file and for the program to then store the data into an Array. The data will then be displayed in a text box. Ultimately, the program will have the ability to plot data to a graph.



GUI Details:




  • 3 Radio buttons - Allows the user to select CSV, XML OR JSON

  • Load File Button

  • Display Button - Displays the data into the textArea

  • Display Graph Button

  • Text Area


Problem: I am having trouble storing the data into an Array. I believe this is because of the format of the data. So for example, this is the first 3 lines of the CSV file:



millis,stamp,datetime,light,temp,vcc
1000, 1273010254, 2010/5/4 21:57:34, 333, 78.32, 3.54
2000, 1273010255, 2010/5/4 21:57:35, 333, 78.32, 3.92
3000, 1273010256, 2010/5/4 21:57:36, 344, 78.32, 3.95


(Note - there are 52789000 lines of data in the CSV/XML/JSON files)



The CSV-Reader Class contains the method for reading through the data, storing it into an array and then storing it to a dataList.



As you can see from the above example, some of the data types are much different. I am having particular trouble with splitting/parsing the time and date variables.



Here is what my CSV-Reader Class code looks like at the moment (Again, I apologise for noob code).



import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class CSVReader {

//create a class that will hold arraylist which will have objects representing all lines of the file

private List<Data> dataList = new ArrayList<Data>();
private String path;

public List<Data> getDataList() {
return dataList;
}

public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}

//Create a method to read through the csv stored in the path
//Create the list of data and store in the dataList

public void readCSV() throws IOException{

//i will create connection with the file, in the path
BufferedReader in = new BufferedReader(new FileReader(path));

String line = null;
line = in.readLine();

while((line = in.readLine())!=null){

//I need to split and store in the temporary variable and create an object


String splits = line.split("\s*(=>|,|\s)\s*");

long millis = Long.parseLong(splits[0].trim());
long stamp = Long.parseLong(splits[1].trim());
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/M/d HH:mm:ss");
System.out.println(splits[2].trim());
LocalDateTime dateTime = LocalDateTime.parse(splits[2].trim(), formatter);
LocalDate dateTime = dateTime.toLocalDate();
LocalTime time = dateTime.toLocalTime();
int light = Integer.parseInt(splits[3].trim());
double temp = Double.parseDouble(splits[4].trim());
double vcc = Double.parseDouble(splits[5].trim());

Data d = new Data(millis,stamp,datetime,light,temp,vcc);//uses constructor


//final job is to add this object 'd' onto the dataList
dataList.add(d);

}//end of while loop

}


Any help would be greatly appreciated!



Edit 1 - I thought that date and time were seperate CSV headers. They were not. Therefore the time variable has been deleted from the program. It has been replaced with the datetime variable.



Edit 2 - My program is now reading the CSV file up until line 15 of the csv




27000, 1273010280, 2010/5/4 21:58:0, 288, 77.74, 3.88




CONSOLE ERRORS



Exception in thread "AWT-EventQueue-0" 
java.time.format.DateTimeParseException: Text **'2010/5/4 21:58:0'** could not
be parsed at index 15
at java.time.format.DateTimeFormatter.parseResolved0(Unknown Source)
at java.time.format.DateTimeFormatter.parse(Unknown Source)
at java.time.LocalDateTime.parse(Unknown Source)
at CSVReader.readCSV(CSVReader.java:55)
at GUI$2.actionPerformed(GUI.java:85)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)









share|improve this question

























  • Why are you trying to parse a date as a double?

    – Joe C
    Nov 25 '18 at 18:05











  • Because I thought that was most suitable...what should it be?

    – rflwill
    Nov 25 '18 at 18:09











  • You may wish to explore the java.time package for appropriate types dealing with dates and times.

    – Joe C
    Nov 25 '18 at 18:18














-1












-1








-1








I am still a beginner with Java programming so I apologise in advance if I am over-complicating my problem.



What is my program?
I am building a GUI based program. The goal of the program is to load a CSV, XML or JSON file and for the program to then store the data into an Array. The data will then be displayed in a text box. Ultimately, the program will have the ability to plot data to a graph.



GUI Details:




  • 3 Radio buttons - Allows the user to select CSV, XML OR JSON

  • Load File Button

  • Display Button - Displays the data into the textArea

  • Display Graph Button

  • Text Area


Problem: I am having trouble storing the data into an Array. I believe this is because of the format of the data. So for example, this is the first 3 lines of the CSV file:



millis,stamp,datetime,light,temp,vcc
1000, 1273010254, 2010/5/4 21:57:34, 333, 78.32, 3.54
2000, 1273010255, 2010/5/4 21:57:35, 333, 78.32, 3.92
3000, 1273010256, 2010/5/4 21:57:36, 344, 78.32, 3.95


(Note - there are 52789000 lines of data in the CSV/XML/JSON files)



The CSV-Reader Class contains the method for reading through the data, storing it into an array and then storing it to a dataList.



As you can see from the above example, some of the data types are much different. I am having particular trouble with splitting/parsing the time and date variables.



Here is what my CSV-Reader Class code looks like at the moment (Again, I apologise for noob code).



import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class CSVReader {

//create a class that will hold arraylist which will have objects representing all lines of the file

private List<Data> dataList = new ArrayList<Data>();
private String path;

public List<Data> getDataList() {
return dataList;
}

public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}

//Create a method to read through the csv stored in the path
//Create the list of data and store in the dataList

public void readCSV() throws IOException{

//i will create connection with the file, in the path
BufferedReader in = new BufferedReader(new FileReader(path));

String line = null;
line = in.readLine();

while((line = in.readLine())!=null){

//I need to split and store in the temporary variable and create an object


String splits = line.split("\s*(=>|,|\s)\s*");

long millis = Long.parseLong(splits[0].trim());
long stamp = Long.parseLong(splits[1].trim());
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/M/d HH:mm:ss");
System.out.println(splits[2].trim());
LocalDateTime dateTime = LocalDateTime.parse(splits[2].trim(), formatter);
LocalDate dateTime = dateTime.toLocalDate();
LocalTime time = dateTime.toLocalTime();
int light = Integer.parseInt(splits[3].trim());
double temp = Double.parseDouble(splits[4].trim());
double vcc = Double.parseDouble(splits[5].trim());

Data d = new Data(millis,stamp,datetime,light,temp,vcc);//uses constructor


//final job is to add this object 'd' onto the dataList
dataList.add(d);

}//end of while loop

}


Any help would be greatly appreciated!



Edit 1 - I thought that date and time were seperate CSV headers. They were not. Therefore the time variable has been deleted from the program. It has been replaced with the datetime variable.



Edit 2 - My program is now reading the CSV file up until line 15 of the csv




27000, 1273010280, 2010/5/4 21:58:0, 288, 77.74, 3.88




CONSOLE ERRORS



Exception in thread "AWT-EventQueue-0" 
java.time.format.DateTimeParseException: Text **'2010/5/4 21:58:0'** could not
be parsed at index 15
at java.time.format.DateTimeFormatter.parseResolved0(Unknown Source)
at java.time.format.DateTimeFormatter.parse(Unknown Source)
at java.time.LocalDateTime.parse(Unknown Source)
at CSVReader.readCSV(CSVReader.java:55)
at GUI$2.actionPerformed(GUI.java:85)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)









share|improve this question
















I am still a beginner with Java programming so I apologise in advance if I am over-complicating my problem.



What is my program?
I am building a GUI based program. The goal of the program is to load a CSV, XML or JSON file and for the program to then store the data into an Array. The data will then be displayed in a text box. Ultimately, the program will have the ability to plot data to a graph.



GUI Details:




  • 3 Radio buttons - Allows the user to select CSV, XML OR JSON

  • Load File Button

  • Display Button - Displays the data into the textArea

  • Display Graph Button

  • Text Area


Problem: I am having trouble storing the data into an Array. I believe this is because of the format of the data. So for example, this is the first 3 lines of the CSV file:



millis,stamp,datetime,light,temp,vcc
1000, 1273010254, 2010/5/4 21:57:34, 333, 78.32, 3.54
2000, 1273010255, 2010/5/4 21:57:35, 333, 78.32, 3.92
3000, 1273010256, 2010/5/4 21:57:36, 344, 78.32, 3.95


(Note - there are 52789000 lines of data in the CSV/XML/JSON files)



The CSV-Reader Class contains the method for reading through the data, storing it into an array and then storing it to a dataList.



As you can see from the above example, some of the data types are much different. I am having particular trouble with splitting/parsing the time and date variables.



Here is what my CSV-Reader Class code looks like at the moment (Again, I apologise for noob code).



import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class CSVReader {

//create a class that will hold arraylist which will have objects representing all lines of the file

private List<Data> dataList = new ArrayList<Data>();
private String path;

public List<Data> getDataList() {
return dataList;
}

public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}

//Create a method to read through the csv stored in the path
//Create the list of data and store in the dataList

public void readCSV() throws IOException{

//i will create connection with the file, in the path
BufferedReader in = new BufferedReader(new FileReader(path));

String line = null;
line = in.readLine();

while((line = in.readLine())!=null){

//I need to split and store in the temporary variable and create an object


String splits = line.split("\s*(=>|,|\s)\s*");

long millis = Long.parseLong(splits[0].trim());
long stamp = Long.parseLong(splits[1].trim());
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/M/d HH:mm:ss");
System.out.println(splits[2].trim());
LocalDateTime dateTime = LocalDateTime.parse(splits[2].trim(), formatter);
LocalDate dateTime = dateTime.toLocalDate();
LocalTime time = dateTime.toLocalTime();
int light = Integer.parseInt(splits[3].trim());
double temp = Double.parseDouble(splits[4].trim());
double vcc = Double.parseDouble(splits[5].trim());

Data d = new Data(millis,stamp,datetime,light,temp,vcc);//uses constructor


//final job is to add this object 'd' onto the dataList
dataList.add(d);

}//end of while loop

}


Any help would be greatly appreciated!



Edit 1 - I thought that date and time were seperate CSV headers. They were not. Therefore the time variable has been deleted from the program. It has been replaced with the datetime variable.



Edit 2 - My program is now reading the CSV file up until line 15 of the csv




27000, 1273010280, 2010/5/4 21:58:0, 288, 77.74, 3.88




CONSOLE ERRORS



Exception in thread "AWT-EventQueue-0" 
java.time.format.DateTimeParseException: Text **'2010/5/4 21:58:0'** could not
be parsed at index 15
at java.time.format.DateTimeFormatter.parseResolved0(Unknown Source)
at java.time.format.DateTimeFormatter.parse(Unknown Source)
at java.time.LocalDateTime.parse(Unknown Source)
at CSVReader.readCSV(CSVReader.java:55)
at GUI$2.actionPerformed(GUI.java:85)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)






java arrays csv parsing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 23:55









usr2564301

17.8k73370




17.8k73370










asked Nov 25 '18 at 17:55









rflwillrflwill

11




11













  • Why are you trying to parse a date as a double?

    – Joe C
    Nov 25 '18 at 18:05











  • Because I thought that was most suitable...what should it be?

    – rflwill
    Nov 25 '18 at 18:09











  • You may wish to explore the java.time package for appropriate types dealing with dates and times.

    – Joe C
    Nov 25 '18 at 18:18



















  • Why are you trying to parse a date as a double?

    – Joe C
    Nov 25 '18 at 18:05











  • Because I thought that was most suitable...what should it be?

    – rflwill
    Nov 25 '18 at 18:09











  • You may wish to explore the java.time package for appropriate types dealing with dates and times.

    – Joe C
    Nov 25 '18 at 18:18

















Why are you trying to parse a date as a double?

– Joe C
Nov 25 '18 at 18:05





Why are you trying to parse a date as a double?

– Joe C
Nov 25 '18 at 18:05













Because I thought that was most suitable...what should it be?

– rflwill
Nov 25 '18 at 18:09





Because I thought that was most suitable...what should it be?

– rflwill
Nov 25 '18 at 18:09













You may wish to explore the java.time package for appropriate types dealing with dates and times.

– Joe C
Nov 25 '18 at 18:18





You may wish to explore the java.time package for appropriate types dealing with dates and times.

– Joe C
Nov 25 '18 at 18:18












4 Answers
4






active

oldest

votes


















1














Since your date column is a string, you need to parse it into a date object. This depends on your version of Java; if you're using Java 8, you could use the new date classes. Maybe this answer could help you along.



It looks like you have two separate columns, date and time in the CSV header, but the lines have the date and time in one column. This could be part of your problem. You need to determine if they are two columns or one, but they can be parsed with the Java libraries in any case.



Also, it would probably be a good idea to use a battle-tested CSV library like Apache Commons CSV, instead of rolling your own CSV parser. You might get by with your own in a limited case, but CSV is not as simple as it first appears.






share|improve this answer
























  • Thank you for pointing that out the >date and >time header problem. I updated my code by deleting the time variable and creating a datetime one instead. I have tried using a CSV library such as OPEN CSV. However, this task has been assigned to me, and my lecturer expects me to do it without a CSV library.

    – rflwill
    Nov 25 '18 at 18:48





















1














ISO 8601




SOLVED So the program was crashing due to my CSV not following the correct date and time format (Read comments below).




When exchanging date-time values as text, use the standard ISO 8601 formats rather than inventing your own. They are wisely designed to be easy to parse by machine and easy to read by humans across cultures. So, 2010-05-04T21:57:34, not 2010/5/4 21:57:34.



The java.time classes use the ISO 8601 formats by default when parsing/generating strings.



Data types



Date-time



The 2nd and 3rd columns of your data feed represent the same thing: a date with time-of-day. The first is a count of whole seconds since the epoch reference date of 1970-01-01T00:00Z (Z means UTC).



So it is silly to include both. As mentioned above, the 3rd column is in a poorly chosen format. The 2nd column approach of using a count-from-epoch is also a poor choice in my opinion, as it is not obvious, no human can decipher its meaning, and so it makes mistakes non-obvious thereby making debugging and logging difficult.



To deal with what we have, the seconds-from-epoch can be parsed as an Instant. This class represents a moment in UTC.



Instant instant = Instant.ofEpochMilli( 1_273_010_254L ) ;


Your 3rd column gives a date and time but omits an indicator of time zone or offset-from-UTC. Since it matches the 2nd column when parsed as seconds from first moment of 1970 in UTC, we know its value was intended for UTC. Omitting such info is bad practice, like having a monetary amount with no indicator of currency.



Ideally both columns should be replaced by a string in ISO 8601 format, for example 2010-05-04T21:57:34Z including the Z to indicate UTC.



If we had to parse the 3rd column without knowing it was intended for UTC, we would parse as a LocalDateTime, a date with time-of-day but lacking a time zone or offset. We need to define a formatting pattern to match your input.



DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu/M/d HH:mm:ss" );
LocalDateTime localDateTime = LocalDateTime.parse( "2010/5/4 21:57:34" , f );


BigDecimal



Your decimal fraction numbers should be represented as BigDecimal objects for accuracy. Never use double/Double or float/Float where you care about accuracy. These types use floating-point technology which trades away accuracy for speed of execution. In contrast, BigDecimal is slow but accurate.



Parse a BigDecimal from a string.



new BigDecimal ( "78.32" ) 


Apache Commons CSV



Do not write code when well-tested code already exists. There are libraries already written to read CSV/Tab-delimited files.



logo for the Apache Commons CSV project



I use Apache Commons CSV for such work. There are several variations of these formats, all handled by this library.



Here is example code. First define a class to hold your data, here named Reading.



Reading.java



package com.basilbourque.example;

import java.math.BigDecimal;
import java.time.Instant;
import java.time.LocalDateTime;

public class Reading {
private Integer millis;
private Instant instant;
private LocalDateTime localDateTime;
private Integer light;
private BigDecimal temp;
private BigDecimal vcc;

public Reading ( Integer millis , Instant instant , LocalDateTime localDateTime , Integer light , BigDecimal temp , BigDecimal vcc ) {
// TODO: Add checks for null arguments: Objects.requireNonNull( … ).
this.millis = millis;
this.instant = instant;
this.localDateTime = localDateTime;
this.light = light;
this.temp = temp;
this.vcc = vcc;
}

@Override
public String toString ( ) {
return "com.basilbourque.example.Reading{" +
"millis=" + millis +
", instant=" + instant +
", localDateTime=" + localDateTime +
", light=" + light +
", temp=" + temp +
", vcc=" + vcc +
'}';
}
}


Example data file:



millis,stamp,datetime,light,temp,vcc
1000, 1273010254, 2010/5/4 21:57:34, 333, 78.32, 3.54
2000, 1273010255, 2010/5/4 21:57:35, 333, 78.32, 3.92
3000, 1273010256, 2010/5/4 21:57:36, 344, 78.32, 3.95


And now call upon Commons CSV to parse that data, instantiate Reading objects, and collect them.



DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu/M/d HH:mm:ss" );

List < Reading > readings = new ArrayList <>( 3 );
Reader reader = null;
try {
reader = new FileReader( "/Users/basilbourque/data.csv" );
Iterable < CSVRecord > records = CSVFormat.RFC4180.withIgnoreSurroundingSpaces( true ).withHeader().parse( reader );
for ( CSVRecord record : records ) {
// Grab inputs
String millisInput = record.get( "millis" );
String stampInput = record.get( "stamp" );
String datetimeInput = record.get( "datetime" );
String lightInput = record.get( "light" );
String tempInput = record.get( "temp" );
String vccInput = record.get( "vcc" );

// Parse inputs
Integer millis = Integer.valueOf( millisInput );
Instant instant = Instant.ofEpochSecond( Integer.valueOf( stampInput ) );
LocalDateTime localDateTime = LocalDateTime.parse( datetimeInput , f );
Integer light = Integer.valueOf( lightInput );
BigDecimal temp = new BigDecimal( tempInput );
BigDecimal vcc = new BigDecimal( vccInput );

// Construct object
Reading r = new Reading( millis , instant , localDateTime , light , temp , vcc );

// Collect object
readings.add( r );
}
} catch ( FileNotFoundException e ) {
e.printStackTrace();
} catch ( IOException e ) {
e.printStackTrace();
}

System.out.println( readings );



[com.basilbourque.example.Reading{millis=1000, instant=2010-05-04T21:57:34Z, localDateTime=2010-05-04T21:57:34, light=333, temp=78.32, vcc=3.54}, com.basilbourque.example.Reading{millis=2000, instant=2010-05-04T21:57:35Z, localDateTime=2010-05-04T21:57:35, light=333, temp=78.32, vcc=3.92}, com.basilbourque.example.Reading{millis=3000, instant=2010-05-04T21:57:36Z, localDateTime=2010-05-04T21:57:36, light=344, temp=78.32, vcc=3.95}]




Regarding your mention:




store the data into an Array




You are using an ArrayList in your code, not an array. See the Oracle Tutorials for lists and for arrays to understand the difference. Generally best to use the Java Collections framework. Where size and speed really matter, we may choose an array.





About java.time



The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.



The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.



To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.



You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.



Where to obtain the java.time classes?





  • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.


    • Java 9 adds some minor features and fixes.




  • Java SE 6 and Java SE 7


    • Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.




  • Android


    • Later versions of Android bundle implementations of the java.time classes.

    • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….




The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.






share|improve this answer

































    0















    1. it seems you have 5 tokens in one line (not 6),


    2. you are trying to parse a date string as a double,



    with some modifications in your code, following one shold work for you:



    String splits = line.split(",");// line.split("\s*(=>|,|\s)\s*");

    long millis = Long.parseLong(splits[0].trim());
    long stamp = Long.parseLong(splits[1].trim());
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/M/d HH:mm:ss");
    LocalDateTime dateTime = LocalDateTime.parse(splits[2].trim(), formatter);
    LocalDate date = dateTime.toLocalDate();
    LocalTime time = dateTime.toLocalTime();
    int light = Integer.parseInt(splits[3].trim());
    double temp = Double.parseDouble(splits[4].trim());
    double vcc = Double.parseDouble(splits[5].trim());





    share|improve this answer


























    • Thanks a lot for your comment! I have incorporated some of your code but I am still struggling with errors. I have updated my original post if you do not mind looking again.

      – rflwill
      Nov 25 '18 at 19:06



















    -1














    You are trying to parse date as numeric and this is a String field, maybe you can use this



    Date date=new SimpleDateFormat("yyyy/M/D HH:mm:ss").parse(splits[2]);


    Now with the date, you can transform as you want






    share|improve this answer
























    • Thanks a lot, I have incorporated this method into my code but I am still having trouble. I have updated my original post if you do not mind looking again, thanks.

      – rflwill
      Nov 25 '18 at 19:07











    • can you try changing yyyy/M/D HH:mm:ss for yyyy/M/D HH:mm:s ?

      – Marco Pens
      Nov 25 '18 at 20:02













    • @rflwill also you need to check if the hours and minutes in your csv are populate with two numbers, if the our is 01 you needt to put HH but if it's only 1 you need to put H the same is with minutes and seconds

      – Marco Pens
      Nov 25 '18 at 20:19











    • The CSV contains dates/times with both single digit and double digits!

      – rflwill
      Nov 25 '18 at 20:29











    • >DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/M/d H:m:s"); Has fixed my code!!! :D thank you for your help

      – rflwill
      Nov 25 '18 at 20:29











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53470278%2fparsing-different-types-of-data-format-from-a-csv-file%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Since your date column is a string, you need to parse it into a date object. This depends on your version of Java; if you're using Java 8, you could use the new date classes. Maybe this answer could help you along.



    It looks like you have two separate columns, date and time in the CSV header, but the lines have the date and time in one column. This could be part of your problem. You need to determine if they are two columns or one, but they can be parsed with the Java libraries in any case.



    Also, it would probably be a good idea to use a battle-tested CSV library like Apache Commons CSV, instead of rolling your own CSV parser. You might get by with your own in a limited case, but CSV is not as simple as it first appears.






    share|improve this answer
























    • Thank you for pointing that out the >date and >time header problem. I updated my code by deleting the time variable and creating a datetime one instead. I have tried using a CSV library such as OPEN CSV. However, this task has been assigned to me, and my lecturer expects me to do it without a CSV library.

      – rflwill
      Nov 25 '18 at 18:48


















    1














    Since your date column is a string, you need to parse it into a date object. This depends on your version of Java; if you're using Java 8, you could use the new date classes. Maybe this answer could help you along.



    It looks like you have two separate columns, date and time in the CSV header, but the lines have the date and time in one column. This could be part of your problem. You need to determine if they are two columns or one, but they can be parsed with the Java libraries in any case.



    Also, it would probably be a good idea to use a battle-tested CSV library like Apache Commons CSV, instead of rolling your own CSV parser. You might get by with your own in a limited case, but CSV is not as simple as it first appears.






    share|improve this answer
























    • Thank you for pointing that out the >date and >time header problem. I updated my code by deleting the time variable and creating a datetime one instead. I have tried using a CSV library such as OPEN CSV. However, this task has been assigned to me, and my lecturer expects me to do it without a CSV library.

      – rflwill
      Nov 25 '18 at 18:48
















    1












    1








    1







    Since your date column is a string, you need to parse it into a date object. This depends on your version of Java; if you're using Java 8, you could use the new date classes. Maybe this answer could help you along.



    It looks like you have two separate columns, date and time in the CSV header, but the lines have the date and time in one column. This could be part of your problem. You need to determine if they are two columns or one, but they can be parsed with the Java libraries in any case.



    Also, it would probably be a good idea to use a battle-tested CSV library like Apache Commons CSV, instead of rolling your own CSV parser. You might get by with your own in a limited case, but CSV is not as simple as it first appears.






    share|improve this answer













    Since your date column is a string, you need to parse it into a date object. This depends on your version of Java; if you're using Java 8, you could use the new date classes. Maybe this answer could help you along.



    It looks like you have two separate columns, date and time in the CSV header, but the lines have the date and time in one column. This could be part of your problem. You need to determine if they are two columns or one, but they can be parsed with the Java libraries in any case.



    Also, it would probably be a good idea to use a battle-tested CSV library like Apache Commons CSV, instead of rolling your own CSV parser. You might get by with your own in a limited case, but CSV is not as simple as it first appears.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 25 '18 at 18:28









    Jere KäpyahoJere Käpyaho

    1,1221825




    1,1221825













    • Thank you for pointing that out the >date and >time header problem. I updated my code by deleting the time variable and creating a datetime one instead. I have tried using a CSV library such as OPEN CSV. However, this task has been assigned to me, and my lecturer expects me to do it without a CSV library.

      – rflwill
      Nov 25 '18 at 18:48





















    • Thank you for pointing that out the >date and >time header problem. I updated my code by deleting the time variable and creating a datetime one instead. I have tried using a CSV library such as OPEN CSV. However, this task has been assigned to me, and my lecturer expects me to do it without a CSV library.

      – rflwill
      Nov 25 '18 at 18:48



















    Thank you for pointing that out the >date and >time header problem. I updated my code by deleting the time variable and creating a datetime one instead. I have tried using a CSV library such as OPEN CSV. However, this task has been assigned to me, and my lecturer expects me to do it without a CSV library.

    – rflwill
    Nov 25 '18 at 18:48







    Thank you for pointing that out the >date and >time header problem. I updated my code by deleting the time variable and creating a datetime one instead. I have tried using a CSV library such as OPEN CSV. However, this task has been assigned to me, and my lecturer expects me to do it without a CSV library.

    – rflwill
    Nov 25 '18 at 18:48















    1














    ISO 8601




    SOLVED So the program was crashing due to my CSV not following the correct date and time format (Read comments below).




    When exchanging date-time values as text, use the standard ISO 8601 formats rather than inventing your own. They are wisely designed to be easy to parse by machine and easy to read by humans across cultures. So, 2010-05-04T21:57:34, not 2010/5/4 21:57:34.



    The java.time classes use the ISO 8601 formats by default when parsing/generating strings.



    Data types



    Date-time



    The 2nd and 3rd columns of your data feed represent the same thing: a date with time-of-day. The first is a count of whole seconds since the epoch reference date of 1970-01-01T00:00Z (Z means UTC).



    So it is silly to include both. As mentioned above, the 3rd column is in a poorly chosen format. The 2nd column approach of using a count-from-epoch is also a poor choice in my opinion, as it is not obvious, no human can decipher its meaning, and so it makes mistakes non-obvious thereby making debugging and logging difficult.



    To deal with what we have, the seconds-from-epoch can be parsed as an Instant. This class represents a moment in UTC.



    Instant instant = Instant.ofEpochMilli( 1_273_010_254L ) ;


    Your 3rd column gives a date and time but omits an indicator of time zone or offset-from-UTC. Since it matches the 2nd column when parsed as seconds from first moment of 1970 in UTC, we know its value was intended for UTC. Omitting such info is bad practice, like having a monetary amount with no indicator of currency.



    Ideally both columns should be replaced by a string in ISO 8601 format, for example 2010-05-04T21:57:34Z including the Z to indicate UTC.



    If we had to parse the 3rd column without knowing it was intended for UTC, we would parse as a LocalDateTime, a date with time-of-day but lacking a time zone or offset. We need to define a formatting pattern to match your input.



    DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu/M/d HH:mm:ss" );
    LocalDateTime localDateTime = LocalDateTime.parse( "2010/5/4 21:57:34" , f );


    BigDecimal



    Your decimal fraction numbers should be represented as BigDecimal objects for accuracy. Never use double/Double or float/Float where you care about accuracy. These types use floating-point technology which trades away accuracy for speed of execution. In contrast, BigDecimal is slow but accurate.



    Parse a BigDecimal from a string.



    new BigDecimal ( "78.32" ) 


    Apache Commons CSV



    Do not write code when well-tested code already exists. There are libraries already written to read CSV/Tab-delimited files.



    logo for the Apache Commons CSV project



    I use Apache Commons CSV for such work. There are several variations of these formats, all handled by this library.



    Here is example code. First define a class to hold your data, here named Reading.



    Reading.java



    package com.basilbourque.example;

    import java.math.BigDecimal;
    import java.time.Instant;
    import java.time.LocalDateTime;

    public class Reading {
    private Integer millis;
    private Instant instant;
    private LocalDateTime localDateTime;
    private Integer light;
    private BigDecimal temp;
    private BigDecimal vcc;

    public Reading ( Integer millis , Instant instant , LocalDateTime localDateTime , Integer light , BigDecimal temp , BigDecimal vcc ) {
    // TODO: Add checks for null arguments: Objects.requireNonNull( … ).
    this.millis = millis;
    this.instant = instant;
    this.localDateTime = localDateTime;
    this.light = light;
    this.temp = temp;
    this.vcc = vcc;
    }

    @Override
    public String toString ( ) {
    return "com.basilbourque.example.Reading{" +
    "millis=" + millis +
    ", instant=" + instant +
    ", localDateTime=" + localDateTime +
    ", light=" + light +
    ", temp=" + temp +
    ", vcc=" + vcc +
    '}';
    }
    }


    Example data file:



    millis,stamp,datetime,light,temp,vcc
    1000, 1273010254, 2010/5/4 21:57:34, 333, 78.32, 3.54
    2000, 1273010255, 2010/5/4 21:57:35, 333, 78.32, 3.92
    3000, 1273010256, 2010/5/4 21:57:36, 344, 78.32, 3.95


    And now call upon Commons CSV to parse that data, instantiate Reading objects, and collect them.



    DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu/M/d HH:mm:ss" );

    List < Reading > readings = new ArrayList <>( 3 );
    Reader reader = null;
    try {
    reader = new FileReader( "/Users/basilbourque/data.csv" );
    Iterable < CSVRecord > records = CSVFormat.RFC4180.withIgnoreSurroundingSpaces( true ).withHeader().parse( reader );
    for ( CSVRecord record : records ) {
    // Grab inputs
    String millisInput = record.get( "millis" );
    String stampInput = record.get( "stamp" );
    String datetimeInput = record.get( "datetime" );
    String lightInput = record.get( "light" );
    String tempInput = record.get( "temp" );
    String vccInput = record.get( "vcc" );

    // Parse inputs
    Integer millis = Integer.valueOf( millisInput );
    Instant instant = Instant.ofEpochSecond( Integer.valueOf( stampInput ) );
    LocalDateTime localDateTime = LocalDateTime.parse( datetimeInput , f );
    Integer light = Integer.valueOf( lightInput );
    BigDecimal temp = new BigDecimal( tempInput );
    BigDecimal vcc = new BigDecimal( vccInput );

    // Construct object
    Reading r = new Reading( millis , instant , localDateTime , light , temp , vcc );

    // Collect object
    readings.add( r );
    }
    } catch ( FileNotFoundException e ) {
    e.printStackTrace();
    } catch ( IOException e ) {
    e.printStackTrace();
    }

    System.out.println( readings );



    [com.basilbourque.example.Reading{millis=1000, instant=2010-05-04T21:57:34Z, localDateTime=2010-05-04T21:57:34, light=333, temp=78.32, vcc=3.54}, com.basilbourque.example.Reading{millis=2000, instant=2010-05-04T21:57:35Z, localDateTime=2010-05-04T21:57:35, light=333, temp=78.32, vcc=3.92}, com.basilbourque.example.Reading{millis=3000, instant=2010-05-04T21:57:36Z, localDateTime=2010-05-04T21:57:36, light=344, temp=78.32, vcc=3.95}]




    Regarding your mention:




    store the data into an Array




    You are using an ArrayList in your code, not an array. See the Oracle Tutorials for lists and for arrays to understand the difference. Generally best to use the Java Collections framework. Where size and speed really matter, we may choose an array.





    About java.time



    The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.



    The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.



    To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.



    You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.



    Where to obtain the java.time classes?





    • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.


      • Java 9 adds some minor features and fixes.




    • Java SE 6 and Java SE 7


      • Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.




    • Android


      • Later versions of Android bundle implementations of the java.time classes.

      • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….




    The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.






    share|improve this answer






























      1














      ISO 8601




      SOLVED So the program was crashing due to my CSV not following the correct date and time format (Read comments below).




      When exchanging date-time values as text, use the standard ISO 8601 formats rather than inventing your own. They are wisely designed to be easy to parse by machine and easy to read by humans across cultures. So, 2010-05-04T21:57:34, not 2010/5/4 21:57:34.



      The java.time classes use the ISO 8601 formats by default when parsing/generating strings.



      Data types



      Date-time



      The 2nd and 3rd columns of your data feed represent the same thing: a date with time-of-day. The first is a count of whole seconds since the epoch reference date of 1970-01-01T00:00Z (Z means UTC).



      So it is silly to include both. As mentioned above, the 3rd column is in a poorly chosen format. The 2nd column approach of using a count-from-epoch is also a poor choice in my opinion, as it is not obvious, no human can decipher its meaning, and so it makes mistakes non-obvious thereby making debugging and logging difficult.



      To deal with what we have, the seconds-from-epoch can be parsed as an Instant. This class represents a moment in UTC.



      Instant instant = Instant.ofEpochMilli( 1_273_010_254L ) ;


      Your 3rd column gives a date and time but omits an indicator of time zone or offset-from-UTC. Since it matches the 2nd column when parsed as seconds from first moment of 1970 in UTC, we know its value was intended for UTC. Omitting such info is bad practice, like having a monetary amount with no indicator of currency.



      Ideally both columns should be replaced by a string in ISO 8601 format, for example 2010-05-04T21:57:34Z including the Z to indicate UTC.



      If we had to parse the 3rd column without knowing it was intended for UTC, we would parse as a LocalDateTime, a date with time-of-day but lacking a time zone or offset. We need to define a formatting pattern to match your input.



      DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu/M/d HH:mm:ss" );
      LocalDateTime localDateTime = LocalDateTime.parse( "2010/5/4 21:57:34" , f );


      BigDecimal



      Your decimal fraction numbers should be represented as BigDecimal objects for accuracy. Never use double/Double or float/Float where you care about accuracy. These types use floating-point technology which trades away accuracy for speed of execution. In contrast, BigDecimal is slow but accurate.



      Parse a BigDecimal from a string.



      new BigDecimal ( "78.32" ) 


      Apache Commons CSV



      Do not write code when well-tested code already exists. There are libraries already written to read CSV/Tab-delimited files.



      logo for the Apache Commons CSV project



      I use Apache Commons CSV for such work. There are several variations of these formats, all handled by this library.



      Here is example code. First define a class to hold your data, here named Reading.



      Reading.java



      package com.basilbourque.example;

      import java.math.BigDecimal;
      import java.time.Instant;
      import java.time.LocalDateTime;

      public class Reading {
      private Integer millis;
      private Instant instant;
      private LocalDateTime localDateTime;
      private Integer light;
      private BigDecimal temp;
      private BigDecimal vcc;

      public Reading ( Integer millis , Instant instant , LocalDateTime localDateTime , Integer light , BigDecimal temp , BigDecimal vcc ) {
      // TODO: Add checks for null arguments: Objects.requireNonNull( … ).
      this.millis = millis;
      this.instant = instant;
      this.localDateTime = localDateTime;
      this.light = light;
      this.temp = temp;
      this.vcc = vcc;
      }

      @Override
      public String toString ( ) {
      return "com.basilbourque.example.Reading{" +
      "millis=" + millis +
      ", instant=" + instant +
      ", localDateTime=" + localDateTime +
      ", light=" + light +
      ", temp=" + temp +
      ", vcc=" + vcc +
      '}';
      }
      }


      Example data file:



      millis,stamp,datetime,light,temp,vcc
      1000, 1273010254, 2010/5/4 21:57:34, 333, 78.32, 3.54
      2000, 1273010255, 2010/5/4 21:57:35, 333, 78.32, 3.92
      3000, 1273010256, 2010/5/4 21:57:36, 344, 78.32, 3.95


      And now call upon Commons CSV to parse that data, instantiate Reading objects, and collect them.



      DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu/M/d HH:mm:ss" );

      List < Reading > readings = new ArrayList <>( 3 );
      Reader reader = null;
      try {
      reader = new FileReader( "/Users/basilbourque/data.csv" );
      Iterable < CSVRecord > records = CSVFormat.RFC4180.withIgnoreSurroundingSpaces( true ).withHeader().parse( reader );
      for ( CSVRecord record : records ) {
      // Grab inputs
      String millisInput = record.get( "millis" );
      String stampInput = record.get( "stamp" );
      String datetimeInput = record.get( "datetime" );
      String lightInput = record.get( "light" );
      String tempInput = record.get( "temp" );
      String vccInput = record.get( "vcc" );

      // Parse inputs
      Integer millis = Integer.valueOf( millisInput );
      Instant instant = Instant.ofEpochSecond( Integer.valueOf( stampInput ) );
      LocalDateTime localDateTime = LocalDateTime.parse( datetimeInput , f );
      Integer light = Integer.valueOf( lightInput );
      BigDecimal temp = new BigDecimal( tempInput );
      BigDecimal vcc = new BigDecimal( vccInput );

      // Construct object
      Reading r = new Reading( millis , instant , localDateTime , light , temp , vcc );

      // Collect object
      readings.add( r );
      }
      } catch ( FileNotFoundException e ) {
      e.printStackTrace();
      } catch ( IOException e ) {
      e.printStackTrace();
      }

      System.out.println( readings );



      [com.basilbourque.example.Reading{millis=1000, instant=2010-05-04T21:57:34Z, localDateTime=2010-05-04T21:57:34, light=333, temp=78.32, vcc=3.54}, com.basilbourque.example.Reading{millis=2000, instant=2010-05-04T21:57:35Z, localDateTime=2010-05-04T21:57:35, light=333, temp=78.32, vcc=3.92}, com.basilbourque.example.Reading{millis=3000, instant=2010-05-04T21:57:36Z, localDateTime=2010-05-04T21:57:36, light=344, temp=78.32, vcc=3.95}]




      Regarding your mention:




      store the data into an Array




      You are using an ArrayList in your code, not an array. See the Oracle Tutorials for lists and for arrays to understand the difference. Generally best to use the Java Collections framework. Where size and speed really matter, we may choose an array.





      About java.time



      The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.



      The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.



      To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.



      You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.



      Where to obtain the java.time classes?





      • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.


        • Java 9 adds some minor features and fixes.




      • Java SE 6 and Java SE 7


        • Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.




      • Android


        • Later versions of Android bundle implementations of the java.time classes.

        • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….




      The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.






      share|improve this answer




























        1












        1








        1







        ISO 8601




        SOLVED So the program was crashing due to my CSV not following the correct date and time format (Read comments below).




        When exchanging date-time values as text, use the standard ISO 8601 formats rather than inventing your own. They are wisely designed to be easy to parse by machine and easy to read by humans across cultures. So, 2010-05-04T21:57:34, not 2010/5/4 21:57:34.



        The java.time classes use the ISO 8601 formats by default when parsing/generating strings.



        Data types



        Date-time



        The 2nd and 3rd columns of your data feed represent the same thing: a date with time-of-day. The first is a count of whole seconds since the epoch reference date of 1970-01-01T00:00Z (Z means UTC).



        So it is silly to include both. As mentioned above, the 3rd column is in a poorly chosen format. The 2nd column approach of using a count-from-epoch is also a poor choice in my opinion, as it is not obvious, no human can decipher its meaning, and so it makes mistakes non-obvious thereby making debugging and logging difficult.



        To deal with what we have, the seconds-from-epoch can be parsed as an Instant. This class represents a moment in UTC.



        Instant instant = Instant.ofEpochMilli( 1_273_010_254L ) ;


        Your 3rd column gives a date and time but omits an indicator of time zone or offset-from-UTC. Since it matches the 2nd column when parsed as seconds from first moment of 1970 in UTC, we know its value was intended for UTC. Omitting such info is bad practice, like having a monetary amount with no indicator of currency.



        Ideally both columns should be replaced by a string in ISO 8601 format, for example 2010-05-04T21:57:34Z including the Z to indicate UTC.



        If we had to parse the 3rd column without knowing it was intended for UTC, we would parse as a LocalDateTime, a date with time-of-day but lacking a time zone or offset. We need to define a formatting pattern to match your input.



        DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu/M/d HH:mm:ss" );
        LocalDateTime localDateTime = LocalDateTime.parse( "2010/5/4 21:57:34" , f );


        BigDecimal



        Your decimal fraction numbers should be represented as BigDecimal objects for accuracy. Never use double/Double or float/Float where you care about accuracy. These types use floating-point technology which trades away accuracy for speed of execution. In contrast, BigDecimal is slow but accurate.



        Parse a BigDecimal from a string.



        new BigDecimal ( "78.32" ) 


        Apache Commons CSV



        Do not write code when well-tested code already exists. There are libraries already written to read CSV/Tab-delimited files.



        logo for the Apache Commons CSV project



        I use Apache Commons CSV for such work. There are several variations of these formats, all handled by this library.



        Here is example code. First define a class to hold your data, here named Reading.



        Reading.java



        package com.basilbourque.example;

        import java.math.BigDecimal;
        import java.time.Instant;
        import java.time.LocalDateTime;

        public class Reading {
        private Integer millis;
        private Instant instant;
        private LocalDateTime localDateTime;
        private Integer light;
        private BigDecimal temp;
        private BigDecimal vcc;

        public Reading ( Integer millis , Instant instant , LocalDateTime localDateTime , Integer light , BigDecimal temp , BigDecimal vcc ) {
        // TODO: Add checks for null arguments: Objects.requireNonNull( … ).
        this.millis = millis;
        this.instant = instant;
        this.localDateTime = localDateTime;
        this.light = light;
        this.temp = temp;
        this.vcc = vcc;
        }

        @Override
        public String toString ( ) {
        return "com.basilbourque.example.Reading{" +
        "millis=" + millis +
        ", instant=" + instant +
        ", localDateTime=" + localDateTime +
        ", light=" + light +
        ", temp=" + temp +
        ", vcc=" + vcc +
        '}';
        }
        }


        Example data file:



        millis,stamp,datetime,light,temp,vcc
        1000, 1273010254, 2010/5/4 21:57:34, 333, 78.32, 3.54
        2000, 1273010255, 2010/5/4 21:57:35, 333, 78.32, 3.92
        3000, 1273010256, 2010/5/4 21:57:36, 344, 78.32, 3.95


        And now call upon Commons CSV to parse that data, instantiate Reading objects, and collect them.



        DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu/M/d HH:mm:ss" );

        List < Reading > readings = new ArrayList <>( 3 );
        Reader reader = null;
        try {
        reader = new FileReader( "/Users/basilbourque/data.csv" );
        Iterable < CSVRecord > records = CSVFormat.RFC4180.withIgnoreSurroundingSpaces( true ).withHeader().parse( reader );
        for ( CSVRecord record : records ) {
        // Grab inputs
        String millisInput = record.get( "millis" );
        String stampInput = record.get( "stamp" );
        String datetimeInput = record.get( "datetime" );
        String lightInput = record.get( "light" );
        String tempInput = record.get( "temp" );
        String vccInput = record.get( "vcc" );

        // Parse inputs
        Integer millis = Integer.valueOf( millisInput );
        Instant instant = Instant.ofEpochSecond( Integer.valueOf( stampInput ) );
        LocalDateTime localDateTime = LocalDateTime.parse( datetimeInput , f );
        Integer light = Integer.valueOf( lightInput );
        BigDecimal temp = new BigDecimal( tempInput );
        BigDecimal vcc = new BigDecimal( vccInput );

        // Construct object
        Reading r = new Reading( millis , instant , localDateTime , light , temp , vcc );

        // Collect object
        readings.add( r );
        }
        } catch ( FileNotFoundException e ) {
        e.printStackTrace();
        } catch ( IOException e ) {
        e.printStackTrace();
        }

        System.out.println( readings );



        [com.basilbourque.example.Reading{millis=1000, instant=2010-05-04T21:57:34Z, localDateTime=2010-05-04T21:57:34, light=333, temp=78.32, vcc=3.54}, com.basilbourque.example.Reading{millis=2000, instant=2010-05-04T21:57:35Z, localDateTime=2010-05-04T21:57:35, light=333, temp=78.32, vcc=3.92}, com.basilbourque.example.Reading{millis=3000, instant=2010-05-04T21:57:36Z, localDateTime=2010-05-04T21:57:36, light=344, temp=78.32, vcc=3.95}]




        Regarding your mention:




        store the data into an Array




        You are using an ArrayList in your code, not an array. See the Oracle Tutorials for lists and for arrays to understand the difference. Generally best to use the Java Collections framework. Where size and speed really matter, we may choose an array.





        About java.time



        The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.



        The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.



        To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.



        You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.



        Where to obtain the java.time classes?





        • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.


          • Java 9 adds some minor features and fixes.




        • Java SE 6 and Java SE 7


          • Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.




        • Android


          • Later versions of Android bundle implementations of the java.time classes.

          • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….




        The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.






        share|improve this answer















        ISO 8601




        SOLVED So the program was crashing due to my CSV not following the correct date and time format (Read comments below).




        When exchanging date-time values as text, use the standard ISO 8601 formats rather than inventing your own. They are wisely designed to be easy to parse by machine and easy to read by humans across cultures. So, 2010-05-04T21:57:34, not 2010/5/4 21:57:34.



        The java.time classes use the ISO 8601 formats by default when parsing/generating strings.



        Data types



        Date-time



        The 2nd and 3rd columns of your data feed represent the same thing: a date with time-of-day. The first is a count of whole seconds since the epoch reference date of 1970-01-01T00:00Z (Z means UTC).



        So it is silly to include both. As mentioned above, the 3rd column is in a poorly chosen format. The 2nd column approach of using a count-from-epoch is also a poor choice in my opinion, as it is not obvious, no human can decipher its meaning, and so it makes mistakes non-obvious thereby making debugging and logging difficult.



        To deal with what we have, the seconds-from-epoch can be parsed as an Instant. This class represents a moment in UTC.



        Instant instant = Instant.ofEpochMilli( 1_273_010_254L ) ;


        Your 3rd column gives a date and time but omits an indicator of time zone or offset-from-UTC. Since it matches the 2nd column when parsed as seconds from first moment of 1970 in UTC, we know its value was intended for UTC. Omitting such info is bad practice, like having a monetary amount with no indicator of currency.



        Ideally both columns should be replaced by a string in ISO 8601 format, for example 2010-05-04T21:57:34Z including the Z to indicate UTC.



        If we had to parse the 3rd column without knowing it was intended for UTC, we would parse as a LocalDateTime, a date with time-of-day but lacking a time zone or offset. We need to define a formatting pattern to match your input.



        DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu/M/d HH:mm:ss" );
        LocalDateTime localDateTime = LocalDateTime.parse( "2010/5/4 21:57:34" , f );


        BigDecimal



        Your decimal fraction numbers should be represented as BigDecimal objects for accuracy. Never use double/Double or float/Float where you care about accuracy. These types use floating-point technology which trades away accuracy for speed of execution. In contrast, BigDecimal is slow but accurate.



        Parse a BigDecimal from a string.



        new BigDecimal ( "78.32" ) 


        Apache Commons CSV



        Do not write code when well-tested code already exists. There are libraries already written to read CSV/Tab-delimited files.



        logo for the Apache Commons CSV project



        I use Apache Commons CSV for such work. There are several variations of these formats, all handled by this library.



        Here is example code. First define a class to hold your data, here named Reading.



        Reading.java



        package com.basilbourque.example;

        import java.math.BigDecimal;
        import java.time.Instant;
        import java.time.LocalDateTime;

        public class Reading {
        private Integer millis;
        private Instant instant;
        private LocalDateTime localDateTime;
        private Integer light;
        private BigDecimal temp;
        private BigDecimal vcc;

        public Reading ( Integer millis , Instant instant , LocalDateTime localDateTime , Integer light , BigDecimal temp , BigDecimal vcc ) {
        // TODO: Add checks for null arguments: Objects.requireNonNull( … ).
        this.millis = millis;
        this.instant = instant;
        this.localDateTime = localDateTime;
        this.light = light;
        this.temp = temp;
        this.vcc = vcc;
        }

        @Override
        public String toString ( ) {
        return "com.basilbourque.example.Reading{" +
        "millis=" + millis +
        ", instant=" + instant +
        ", localDateTime=" + localDateTime +
        ", light=" + light +
        ", temp=" + temp +
        ", vcc=" + vcc +
        '}';
        }
        }


        Example data file:



        millis,stamp,datetime,light,temp,vcc
        1000, 1273010254, 2010/5/4 21:57:34, 333, 78.32, 3.54
        2000, 1273010255, 2010/5/4 21:57:35, 333, 78.32, 3.92
        3000, 1273010256, 2010/5/4 21:57:36, 344, 78.32, 3.95


        And now call upon Commons CSV to parse that data, instantiate Reading objects, and collect them.



        DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu/M/d HH:mm:ss" );

        List < Reading > readings = new ArrayList <>( 3 );
        Reader reader = null;
        try {
        reader = new FileReader( "/Users/basilbourque/data.csv" );
        Iterable < CSVRecord > records = CSVFormat.RFC4180.withIgnoreSurroundingSpaces( true ).withHeader().parse( reader );
        for ( CSVRecord record : records ) {
        // Grab inputs
        String millisInput = record.get( "millis" );
        String stampInput = record.get( "stamp" );
        String datetimeInput = record.get( "datetime" );
        String lightInput = record.get( "light" );
        String tempInput = record.get( "temp" );
        String vccInput = record.get( "vcc" );

        // Parse inputs
        Integer millis = Integer.valueOf( millisInput );
        Instant instant = Instant.ofEpochSecond( Integer.valueOf( stampInput ) );
        LocalDateTime localDateTime = LocalDateTime.parse( datetimeInput , f );
        Integer light = Integer.valueOf( lightInput );
        BigDecimal temp = new BigDecimal( tempInput );
        BigDecimal vcc = new BigDecimal( vccInput );

        // Construct object
        Reading r = new Reading( millis , instant , localDateTime , light , temp , vcc );

        // Collect object
        readings.add( r );
        }
        } catch ( FileNotFoundException e ) {
        e.printStackTrace();
        } catch ( IOException e ) {
        e.printStackTrace();
        }

        System.out.println( readings );



        [com.basilbourque.example.Reading{millis=1000, instant=2010-05-04T21:57:34Z, localDateTime=2010-05-04T21:57:34, light=333, temp=78.32, vcc=3.54}, com.basilbourque.example.Reading{millis=2000, instant=2010-05-04T21:57:35Z, localDateTime=2010-05-04T21:57:35, light=333, temp=78.32, vcc=3.92}, com.basilbourque.example.Reading{millis=3000, instant=2010-05-04T21:57:36Z, localDateTime=2010-05-04T21:57:36, light=344, temp=78.32, vcc=3.95}]




        Regarding your mention:




        store the data into an Array




        You are using an ArrayList in your code, not an array. See the Oracle Tutorials for lists and for arrays to understand the difference. Generally best to use the Java Collections framework. Where size and speed really matter, we may choose an array.





        About java.time



        The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.



        The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.



        To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.



        You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.



        Where to obtain the java.time classes?





        • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.


          • Java 9 adds some minor features and fixes.




        • Java SE 6 and Java SE 7


          • Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.




        • Android


          • Later versions of Android bundle implementations of the java.time classes.

          • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….




        The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 26 '18 at 7:13

























        answered Nov 25 '18 at 23:53









        Basil BourqueBasil Bourque

        113k29388547




        113k29388547























            0















            1. it seems you have 5 tokens in one line (not 6),


            2. you are trying to parse a date string as a double,



            with some modifications in your code, following one shold work for you:



            String splits = line.split(",");// line.split("\s*(=>|,|\s)\s*");

            long millis = Long.parseLong(splits[0].trim());
            long stamp = Long.parseLong(splits[1].trim());
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/M/d HH:mm:ss");
            LocalDateTime dateTime = LocalDateTime.parse(splits[2].trim(), formatter);
            LocalDate date = dateTime.toLocalDate();
            LocalTime time = dateTime.toLocalTime();
            int light = Integer.parseInt(splits[3].trim());
            double temp = Double.parseDouble(splits[4].trim());
            double vcc = Double.parseDouble(splits[5].trim());





            share|improve this answer


























            • Thanks a lot for your comment! I have incorporated some of your code but I am still struggling with errors. I have updated my original post if you do not mind looking again.

              – rflwill
              Nov 25 '18 at 19:06
















            0















            1. it seems you have 5 tokens in one line (not 6),


            2. you are trying to parse a date string as a double,



            with some modifications in your code, following one shold work for you:



            String splits = line.split(",");// line.split("\s*(=>|,|\s)\s*");

            long millis = Long.parseLong(splits[0].trim());
            long stamp = Long.parseLong(splits[1].trim());
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/M/d HH:mm:ss");
            LocalDateTime dateTime = LocalDateTime.parse(splits[2].trim(), formatter);
            LocalDate date = dateTime.toLocalDate();
            LocalTime time = dateTime.toLocalTime();
            int light = Integer.parseInt(splits[3].trim());
            double temp = Double.parseDouble(splits[4].trim());
            double vcc = Double.parseDouble(splits[5].trim());





            share|improve this answer


























            • Thanks a lot for your comment! I have incorporated some of your code but I am still struggling with errors. I have updated my original post if you do not mind looking again.

              – rflwill
              Nov 25 '18 at 19:06














            0












            0








            0








            1. it seems you have 5 tokens in one line (not 6),


            2. you are trying to parse a date string as a double,



            with some modifications in your code, following one shold work for you:



            String splits = line.split(",");// line.split("\s*(=>|,|\s)\s*");

            long millis = Long.parseLong(splits[0].trim());
            long stamp = Long.parseLong(splits[1].trim());
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/M/d HH:mm:ss");
            LocalDateTime dateTime = LocalDateTime.parse(splits[2].trim(), formatter);
            LocalDate date = dateTime.toLocalDate();
            LocalTime time = dateTime.toLocalTime();
            int light = Integer.parseInt(splits[3].trim());
            double temp = Double.parseDouble(splits[4].trim());
            double vcc = Double.parseDouble(splits[5].trim());





            share|improve this answer
















            1. it seems you have 5 tokens in one line (not 6),


            2. you are trying to parse a date string as a double,



            with some modifications in your code, following one shold work for you:



            String splits = line.split(",");// line.split("\s*(=>|,|\s)\s*");

            long millis = Long.parseLong(splits[0].trim());
            long stamp = Long.parseLong(splits[1].trim());
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/M/d HH:mm:ss");
            LocalDateTime dateTime = LocalDateTime.parse(splits[2].trim(), formatter);
            LocalDate date = dateTime.toLocalDate();
            LocalTime time = dateTime.toLocalTime();
            int light = Integer.parseInt(splits[3].trim());
            double temp = Double.parseDouble(splits[4].trim());
            double vcc = Double.parseDouble(splits[5].trim());






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 25 '18 at 19:00

























            answered Nov 25 '18 at 18:33









            guleryuzguleryuz

            2,0431715




            2,0431715













            • Thanks a lot for your comment! I have incorporated some of your code but I am still struggling with errors. I have updated my original post if you do not mind looking again.

              – rflwill
              Nov 25 '18 at 19:06



















            • Thanks a lot for your comment! I have incorporated some of your code but I am still struggling with errors. I have updated my original post if you do not mind looking again.

              – rflwill
              Nov 25 '18 at 19:06

















            Thanks a lot for your comment! I have incorporated some of your code but I am still struggling with errors. I have updated my original post if you do not mind looking again.

            – rflwill
            Nov 25 '18 at 19:06





            Thanks a lot for your comment! I have incorporated some of your code but I am still struggling with errors. I have updated my original post if you do not mind looking again.

            – rflwill
            Nov 25 '18 at 19:06











            -1














            You are trying to parse date as numeric and this is a String field, maybe you can use this



            Date date=new SimpleDateFormat("yyyy/M/D HH:mm:ss").parse(splits[2]);


            Now with the date, you can transform as you want






            share|improve this answer
























            • Thanks a lot, I have incorporated this method into my code but I am still having trouble. I have updated my original post if you do not mind looking again, thanks.

              – rflwill
              Nov 25 '18 at 19:07











            • can you try changing yyyy/M/D HH:mm:ss for yyyy/M/D HH:mm:s ?

              – Marco Pens
              Nov 25 '18 at 20:02













            • @rflwill also you need to check if the hours and minutes in your csv are populate with two numbers, if the our is 01 you needt to put HH but if it's only 1 you need to put H the same is with minutes and seconds

              – Marco Pens
              Nov 25 '18 at 20:19











            • The CSV contains dates/times with both single digit and double digits!

              – rflwill
              Nov 25 '18 at 20:29











            • >DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/M/d H:m:s"); Has fixed my code!!! :D thank you for your help

              – rflwill
              Nov 25 '18 at 20:29
















            -1














            You are trying to parse date as numeric and this is a String field, maybe you can use this



            Date date=new SimpleDateFormat("yyyy/M/D HH:mm:ss").parse(splits[2]);


            Now with the date, you can transform as you want






            share|improve this answer
























            • Thanks a lot, I have incorporated this method into my code but I am still having trouble. I have updated my original post if you do not mind looking again, thanks.

              – rflwill
              Nov 25 '18 at 19:07











            • can you try changing yyyy/M/D HH:mm:ss for yyyy/M/D HH:mm:s ?

              – Marco Pens
              Nov 25 '18 at 20:02













            • @rflwill also you need to check if the hours and minutes in your csv are populate with two numbers, if the our is 01 you needt to put HH but if it's only 1 you need to put H the same is with minutes and seconds

              – Marco Pens
              Nov 25 '18 at 20:19











            • The CSV contains dates/times with both single digit and double digits!

              – rflwill
              Nov 25 '18 at 20:29











            • >DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/M/d H:m:s"); Has fixed my code!!! :D thank you for your help

              – rflwill
              Nov 25 '18 at 20:29














            -1












            -1








            -1







            You are trying to parse date as numeric and this is a String field, maybe you can use this



            Date date=new SimpleDateFormat("yyyy/M/D HH:mm:ss").parse(splits[2]);


            Now with the date, you can transform as you want






            share|improve this answer













            You are trying to parse date as numeric and this is a String field, maybe you can use this



            Date date=new SimpleDateFormat("yyyy/M/D HH:mm:ss").parse(splits[2]);


            Now with the date, you can transform as you want







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 25 '18 at 18:36









            Marco PensMarco Pens

            916




            916













            • Thanks a lot, I have incorporated this method into my code but I am still having trouble. I have updated my original post if you do not mind looking again, thanks.

              – rflwill
              Nov 25 '18 at 19:07











            • can you try changing yyyy/M/D HH:mm:ss for yyyy/M/D HH:mm:s ?

              – Marco Pens
              Nov 25 '18 at 20:02













            • @rflwill also you need to check if the hours and minutes in your csv are populate with two numbers, if the our is 01 you needt to put HH but if it's only 1 you need to put H the same is with minutes and seconds

              – Marco Pens
              Nov 25 '18 at 20:19











            • The CSV contains dates/times with both single digit and double digits!

              – rflwill
              Nov 25 '18 at 20:29











            • >DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/M/d H:m:s"); Has fixed my code!!! :D thank you for your help

              – rflwill
              Nov 25 '18 at 20:29



















            • Thanks a lot, I have incorporated this method into my code but I am still having trouble. I have updated my original post if you do not mind looking again, thanks.

              – rflwill
              Nov 25 '18 at 19:07











            • can you try changing yyyy/M/D HH:mm:ss for yyyy/M/D HH:mm:s ?

              – Marco Pens
              Nov 25 '18 at 20:02













            • @rflwill also you need to check if the hours and minutes in your csv are populate with two numbers, if the our is 01 you needt to put HH but if it's only 1 you need to put H the same is with minutes and seconds

              – Marco Pens
              Nov 25 '18 at 20:19











            • The CSV contains dates/times with both single digit and double digits!

              – rflwill
              Nov 25 '18 at 20:29











            • >DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/M/d H:m:s"); Has fixed my code!!! :D thank you for your help

              – rflwill
              Nov 25 '18 at 20:29

















            Thanks a lot, I have incorporated this method into my code but I am still having trouble. I have updated my original post if you do not mind looking again, thanks.

            – rflwill
            Nov 25 '18 at 19:07





            Thanks a lot, I have incorporated this method into my code but I am still having trouble. I have updated my original post if you do not mind looking again, thanks.

            – rflwill
            Nov 25 '18 at 19:07













            can you try changing yyyy/M/D HH:mm:ss for yyyy/M/D HH:mm:s ?

            – Marco Pens
            Nov 25 '18 at 20:02







            can you try changing yyyy/M/D HH:mm:ss for yyyy/M/D HH:mm:s ?

            – Marco Pens
            Nov 25 '18 at 20:02















            @rflwill also you need to check if the hours and minutes in your csv are populate with two numbers, if the our is 01 you needt to put HH but if it's only 1 you need to put H the same is with minutes and seconds

            – Marco Pens
            Nov 25 '18 at 20:19





            @rflwill also you need to check if the hours and minutes in your csv are populate with two numbers, if the our is 01 you needt to put HH but if it's only 1 you need to put H the same is with minutes and seconds

            – Marco Pens
            Nov 25 '18 at 20:19













            The CSV contains dates/times with both single digit and double digits!

            – rflwill
            Nov 25 '18 at 20:29





            The CSV contains dates/times with both single digit and double digits!

            – rflwill
            Nov 25 '18 at 20:29













            >DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/M/d H:m:s"); Has fixed my code!!! :D thank you for your help

            – rflwill
            Nov 25 '18 at 20:29





            >DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/M/d H:m:s"); Has fixed my code!!! :D thank you for your help

            – rflwill
            Nov 25 '18 at 20:29


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53470278%2fparsing-different-types-of-data-format-from-a-csv-file%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'