• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Adding values into array from csv file

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
It's a beautiful day in this neighborhood - Fred Rogers. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic