• 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

reaind text file and finding column avg

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanx alot for ur help.i got it
 
The only thing that kept the leeches off of me was this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic