-
1 Attachment(s)
I can't isolate the first four colums y,m,d,t
I'm trying to get the year, month, day and time isolated so I can then I can make it populate in a different format.
Code Java:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class CSVDateandTime {
public static void main(String [] args) {
String fileName = "solar.all";
File file = new File(fileName);
try {
Scanner inputStream = new Scanner(file);
while (inputStream.hasNext()){
String data = inputStream.next(); //gets a whole line
String [] values = data.split(",");
System.out.println(data + "***");
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Attachment 1442
-
Re: I can't isolate the first four colums y,m,d,t
Can you post the console from when you execute the code that shows what it is doing?
Add a println() to print out the value of data before it is split.
-
1 Attachment(s)
Re: I can't isolate the first four colums y,m,d,t
I'm using Eclipse and i attached a pic of the problem I'm having.
Attachment 1441
-
Re: I can't isolate the first four colums y,m,d,t
I also attached the file that I was pulling my data.
-
Re: I can't isolate the first four colums y,m,d,t
Where in the posted code is an array indexed?
Can you copy and paste here the full text of the error message? You can't copy and paste from an image.
The code should make sure the index does not go past the end of the array.
Remember array indexes start at 0 and go to the length-1.
-
Re: I can't isolate the first four colums y,m,d,t
I do not have an array and that's what's throwing me off. I'm using what I learned and it should show me the first column but it's not.
Code Java:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class CSVDateandTime {
public static void main(String [] args) {
String fileName = "solar.all";
File file = new File(fileName);
try {
Scanner inputStream = new Scanner(file);
while (inputStream.hasNext()){
String data = inputStream.next(); //gets a whole line
String [] values = data.split(",");
System.out.println(values [1] + "***");
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
m***
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at CSVDateandTime.main(CSVDateandTime.java:17)
-
Re: I can't isolate the first four colums y,m,d,t
Quote:
I do not have an array
Code :
String [] values =
...
System.out.println(values [1] + "***");
values is an array.
How do know that the values array has two elements? 1 is the index for second element
Where is the print out of the data variable? For debugging you need to do that to show you what is being read.
-
Re: I can't isolate the first four colums y,m,d,t
I guess that's where I'm lost at. I didn't make note of what values meant. Could you help me with pulling out four columns which are the year, month, date and time so I can use that create something else.
-
Re: I can't isolate the first four colums y,m,d,t
What is printed out when you print out the value of the variable: data?
Is is what you expect to see?
-
Re: I can't isolate the first four colums y,m,d,t
Yes that correct. Now trying to get the first 4 columns to display is my current goal.
-
Re: I can't isolate the first four colums y,m,d,t
What prints out when you print out the value of the variable: data?
Can you copy and paste here what is printed?
-
Re: I can't isolate the first four colums y,m,d,t
I fixed the problem but now trying to figure out how to isolate the specific columns.
Code Java:
while (inputStream.hasNext()){
String data = inputStream.next();
System.out.println ( data ) ;
String [] values = data.split( ",");
System.out.println(values [0] + "*");
}
This is what it prints after the fix.
y,m,d,t,Incremental Generation Energy (kWh),Incremental Building Demand (kWh),Incremental Insolation (kWh/M^2),Last Generation Energy (kWh),Last Insolation (kWh/M^2),Average Generation Power (kW),Average Irradiance (W/M^2),Average Ambient Temperature (C),Average Cell Temperature (C)
y*
2012,8,11,00:00,-0.258,null,null,412854.167,null,-0.243,null,null,null
2012*
How would I get it to just show the y,m,d,t?
-
Re: I can't isolate the first four colums y,m,d,t
Quote:
get it to just show the y,m,d,t?
Can you post what you want printed? If the values array has 4 or more elements do you want to see the first 4 elements? To see the contents of the values array use the Arrays class's toString method somethng like this:
Code :
System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
The posted code has two println() statements. What is printed by the first one and what is printed by the second one? Add an ID String to each so that you can tell:
Code :
System.out.println ("data="+ data + "<" ) ;
...
System.out.println("Values[0]="+values [0] + "*");
-
Re: I can't isolate the first four colums y,m,d,t
I just want the first 4 elements. I'm trying to get it so I can then turn the y,m,d, t to a code to read it in a standard java day time format.
-
Re: I can't isolate the first four colums y,m,d,t
Quote:
I just want the first 4 elements.
Check if values has 4 or more elements. If it does, get the first 4 elements for your data.
-
Re: I can't isolate the first four colums y,m,d,t
I'll try that. I think you gave me enough info to work with and figure out the next step.