Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

fileReader

 
kelvin cheung
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want to make a program that can count the :
[YEAR] and the [Celsius Degree] of that year.
it will have to find those information on a .txt file which look something like :
1871
3.1
1872
4.8
1873
4.7
1874
4.3
it will also have to find the MAX, Avarage and the MIN temp.
and also have to count how many it is registered.
------------------------------------------------------
i wonder how to write the program that knows that the first 1 is a YEAR,
And the second is a DEGREE? and then count them as one! ??
 
Elouise Kivineva
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You read the file as a String, then tokenize it. Every odd token (the 1st, 3rd, 5th, ...) is a year and every even token (2nd, 4th, ...) is a temperature - until you run out of tokens.
You might want to read up on the String & StringTokenizer classes in the Java API.
 
kelvin cheung
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Elouise,
how do i count odd and even tokens?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think with your year & date information on separate lines you can use readLine() instead of tokenizing. Take a look at the doc on BufferedReader and readLine().
Start sketching out the highest level logic of your program. Maybe:

Ooops, I hope I haven't done more of your homework than I should. Does that give you some ideas? For fun, maybe see if you can write a Java method with exactly those lines turned into code.
 
Elouise Kivineva
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you count even and odd tokens?
It's not really counting, but for instance you could read using a loop that reads first the year, then the temperature. On the next round it reads first the year, then the temp. Etc., etc., as long as there are tokens (or token pairs) to read.
 
kelvin cheung
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the replies.
but i got some problems with the fileReading :

it only prints "1871" out, over and over again.... why ?
 
Alex McCormick
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm taking a bit of a wild stab here as I haven't done file reading in a while, but don't you have to include your readLine() in the loop? Otherwise I think it doesn't know to read the next line, which is why it keeps printing 1871 over and over.
 
kelvin cheung
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you alex ..it worked
like this
 
kelvin cheung
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok...i have tried but,...
i still dont know how to distinguish the years and the temps,
and pair them as 1.
any ideas?
 
Elouise Kivineva
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
kelvin cheung
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you Elouise Kivineva
for taking your time to tell me how to do.
i finally did it ..
my next thing to do is to make a GUI version of this.
i will try myself. if i find some difficulties, i will ask you guys.
take care
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic