Hi,
I have a csv file that looks like this:
Date, Cost
Jan 12, 23.2
Feb 2, 45.3
May 4, 33.4
March 3, 32
May 9, 21
July 22, 332
Aug 3, 765
I am using scanner to read the file and can do that with no issues. Howevet, I only need to read the second column of the file (i.e the cost column).
My code looks like this:
Scanner sc = new scanner (file);
while (sc.hasNext()){
String data = sc.next();
string[] val = data.split(",");
System.out.println(values[0]);
}
This prints out:
Date
Jan
12
Feb
2
May
4
March
3
May
9
July
22
Aug
3
And if I change the print statement to
System.out.println(values[1]);
I get an out of bounds exception. What am I doing wrong?
How can I write this to only print the second column?
Is the issue because there is a space between the month and date? how can I get rid of it?