File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes fileReader Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "fileReader" Watch "fileReader" New topic
Author

fileReader

kelvin cheung
Ranch Hand

Joined: Mar 27, 2004
Posts: 120
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

Joined: Feb 07, 2002
Posts: 154
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

Joined: Mar 27, 2004
Posts: 120
hi Elouise,
how do i count odd and even tokens?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Elouise Kivineva
Ranch Hand

Joined: Feb 07, 2002
Posts: 154
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

Joined: Mar 27, 2004
Posts: 120
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

Joined: Mar 08, 2003
Posts: 31
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

Joined: Mar 27, 2004
Posts: 120
thank you alex ..it worked
like this
kelvin cheung
Ranch Hand

Joined: Mar 27, 2004
Posts: 120
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

Joined: Feb 07, 2002
Posts: 154
kelvin cheung
Ranch Hand

Joined: Mar 27, 2004
Posts: 120
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: fileReader