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?

Recommended Answers

All 10 Replies

It seems that there are some "." separated values that only has one column,thats why it is returning out of bound exception.
Do one thing ,instead of

System.out.println(values[0]);

Frist print length of array to see if your input is correct

 System.out.println(value.length);

Check if it is less than 2 at any place.If it is less than 2 then you can later on make a condition check if length less than 2 than continue otherwise print the 2nd element of array

if(value.length<2)
continue;
else
System.out.println(value[1]);

I hope it will solve your problem

aha, it looks like the issue is because of the space. when I print the values, i get:
2
1
2
1
2
1
2
1
2

so when i print out using
if(value.length==2)
System.out.println(values[0]), i get:

Date
12
2
4
3
9
22
3

and for System.out.println(values[1), i get:

23.2
45.3
33.4
32
21
332
765

Is there a way to print out the corresponding month also?I need to copy this data to arrays, so whatever is in values[0] looks like Jan 12, and values[1] contains its corresponding cost.

As per what i can see you must get Jan 12 as value[0],are you sure the data is Jan 12 in CSV.It might be 12 only,othewise it must have printed both

if value.length == 2, value[0] gives me 12, and value[1] gives 23.2
if value.length == 1 value[0] gives jan etc.
I think somehow the space is getting read as a delimiter? Looks like att the moment, its reading the works seperated by a space seperately?

Scanners are just a simple fix for easy input, if you try to use them for formatted data you run out of steam really quickly. Here you have a scanner delimiting on blanks (default behaviour), but you want to keep month and day together.
You will find this easier if you just take full control yourself. Just read the file one complete line at a time (Scanner's nextLine() is OK for that), then use split(",") to separate the dates and costs. If you later need to separate the month and day you can split the date on " ".

Your code is correct with the difference that you need to call the nextLine:

Scanner sc = new scanner (file);
while (sc.hasNext()){
String data = sc.nextLine();

String[] val = data.split(",");
System.out.println(values[0] + " " + values[1]);

}

didn't downvote, just replying on the post above that:

you also need to remember that if you use the name val in the declaration of the array, you should also use the name val (not values) when trying to print it's contents.

typo mistake

true, but it's being addopted in the suggested solutions as well.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.