• Post Reply Bookmark Topic Watch Topic
  • New Topic

reaind text file and finding column avg

 
sina milka
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
i have a txt file like this:
gold dilver bronze
dept1 1.25 2.25 7,35
dept2 0.25 2.95 9,35
dept3 1.55 8.25 8,35
dept4 1.65 2.25 8,35
..........
...............
........
i have to read this file and find the avg of gold and silver and bronze.my coding part is like i used a string tokeniser and read tokens but stuck with the current position.my coding:
import java.io.*;
import java.util.*;
public class formula
{

/* String rms=" ";
String rank=" ";
String n1=" ";
String n1eq=" ";*/
public static void main(String s[]) throws Exception
{
int ln=0;
String line=" ";String token="hi ";
BufferedReader b = new BufferedReader(new FileReader("sample.txt"));
LineNumberReader l = new LineNumberReader(b);
StringTokenizer st = new StringTokenizer(line);
while(true)
{
line = l.readLine();if (line==null) break;
ln = l.getLineNumber();
if (ln>=2)
{
System.out.println(line);
while (st.hasMoreTokens())
{
token = st.nextToken();System.out.println(token);
}

}
}
pls help me out.i am new to this and also suggest would it be better to use string tokeniser or stream tokeniser.
thanx in advance to one and all.
 
Stephen Huey
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what your problem is--you seem to be on the right track. However, since StringTokenizer is deprecated, I'd recommend using String's newer split method to break up each line into parts--it does at least a good a job, or better!
The split method turns each line into an array of Strings, and you could know that the 1st one is always gold, the second is always silver, etc. Have 3 Lists or something like that--one could be a GoldList, another a SilverList, etc. Whenever you're in the first array element, you always add to gold's list, etc etc...at the end you just add up all the gold list elements and average them...get the picture?
There are tons of ways to do it, many of them simpler or faster or whatever, but hopefully you got the jist...
 
Joe Ess
Bartender
Posts: 9361
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by S. C. Huey:
However, since StringTokenizer is deprecated. . .

In what API? It's not depreciated in J2SE 1.4.2!
 
sina milka
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx alot for ur help.i got it
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!