The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
December 2000

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Anyone seen a line of text???

Posted by Oz on December 10, 2000 at 3:49 PM

I have to read text from a file,
count the number of words in the text.
And in a method, count the number of vowels in the text and
the number of words that contain exactly 2 vowels.

Heres what I have so far. Can't get it into a method or to
count words with 2 vowels and I've lost a line of text and my
hair!! The file should read these 3 lines:
Mary had a little lamb
its fleece was white as snow
Understand

Please help.Oz

import java.util.*;
import java.io.*;
import UU.*;


public class mary3{
/*This class reads from a file and prints out details.*/

public static void main(String[]args)throws IOException {

String word, mary;

int count=0,vowels=0;
BufferedReader inFile = Text.open("a:mary.dat");//reads from file

mary = inFile.readLine();
word = inFile.readLine();

while (mary != null){
StringTokenizer t=new StringTokenizer (mary);
while (t.hasMoreTokens()){
mary=t.nextToken();
count++;
System.out.print(mary+" ");

}//end while


System.out.println();
mary = inFile.readLine();

}//end while
//inFile.close();
System.out.println();
System.out.print("There are "+count + " words in the text");
System.out.println();



while (word != null){
StringTokenizer w=new StringTokenizer (word);
while (w.hasMoreTokens()){
word=w.nextToken();
for(int i = 0; i < word.length(); i++){
switch(word.charAt(i)){
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
vowels++;
break;
default:
break;
} //end of for
} //end of switch



}//end inner while



System.out.println();
word = inFile.readLine();



}//end outside while




inFile.close();
//System.out.println();
//System.out.print("There are "+count + " words in the text");
System.out.println();
System.out.println("Total vowels = " + vowels);
System.out.println();

}//end of main
}//end of class

Output is:
Mary had a little lamb
Understand

There are 6 words in the text


Total vowels = 9

Process Exit...





Replies:
  • I saw it... Kishori Sharan December 10, 2000 at 8:27 PM (0)

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us