• Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding values into array from csv file  RSS feed

 
misheck Mberi
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having a problem trying to write a method that can add values from a cvs file row by row. The problem is the csv file is structure in this way

1980 11 14 16 23 18 .....
1981 21 17 16 12 18 .....
1982 14 16 23 18 17 .....
1983 17 16 12 18 10 ......

I need to add the values after year and get an average for each year. My current knowledge of java only tells me how to add the whole row. Eventually after I have finished writing my program I am suppose to be able to type in a year and the get the average values for that year.

How can I add the values in row ignoring the first row and looping through a very long csv file with values stretching from 1900 to 2013?
 
Paweł Baczyński
Bartender
Posts: 2085
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Learn how to read single line of a file (look for BufferedReader and FileReader).
2. Learn how to split a string given delimiter (a space in your case I think). Look at String.split method.
3. Learn how to "change" String to integer. It's called parsing. Look at Integer.parseInt method.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

By cvs do you mean csv? You might do better to search, if you intend to use this other than a training exercise. There are programs, which I am not familiar with, specially designed for reading csv files.
 
misheck Mberi
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Pawel - I will look at the topics you have mentioned, I have had a rough look already but I have noticed I need more time to get my program to work.

@Campbell - I mean a csv file and I am using the java built in scanner to read the files.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How many tokens are there on a line? Is it always the same number?
 
misheck Mberi
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:How many tokens are there on a line? Is it always the same number?
There is always 12 and I have already set a static variable with the value of 12.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!