Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

word count, longest , shortest word in text file  RSS feed

 
Randy Helene
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.
 
Dirk Schreckmann
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.
 
Michael Matola
whippersnapper
Ranch Hand
Posts: 1838
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.
 
Thomas Paul
mister krabs
Ranch Hand
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);
 
David Weitzman
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
Ranch Hand
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!