• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

word count, longest , shortest word in text file

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need some help on an assignment using uses FileInputStream, FileOutputStream, File classes. I need to open a text file containing several lines of text, count the number of words (separated by one or more spaces) and find the longest and shortest word. The summary should be printed at the console. I could use some help on how to go about this.
Thanks for your help.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, what do you know how to do so far?
Do you know how to open and read the contents of a text file?
 
Randy Helene
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I forgot to include this. I can open the file and read it. Here is the code I have so far:
<CODE>
import java.io.*;
class FileI {
public static void main(String args[]) throws Exception {
FileInputStream fi=new FileInputStream("liberty.txt");
int ibytes = fi.available();
for (int i=0; i<ibytes; i++){
System.out.print((char)fi.read() );
}
fi.close();
}
}
</CODE>
 
Randy Helene
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone help me with this please?
Thank you.
 
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Randy Helene:
count the number of words (separated by one or more spaces)


Take a look at StreamTokenizer or StringTokenizer.
and find the longest and shortest word.
Take a look at String.
 
Randy Helene
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.
 
Randy Helene
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still need help. I tested out StreamTokenizer and it worked when I provided a string but it did not work with FileInputStream, counting the words in a file. Any other suggestions?
Thanks.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try something like this:
Reader r = new BufferedReader(new FileReader("liberty.txt"));
StreamTokenizer st = new StreamTokenizer(r);
 
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The StreamTokenizer class is evil. It has a bizarre interface and poorly documented behavior. If you want to use a tokenizer, I recommend sticking to StringTokenizer.
StringTokenizer tok = new StringTokenizer(bufferedReader.readLine(), " ");
The only functions you ought to need after that are hasMoreTokens() and nextToken().
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The StreamTokenizer is also a well known object hog manufacturing thousands of objects for umknown reasons. It was roundly condemned in the book, "Java Performance Tuning".
But I figured for a homework assignment it would be fine.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!