Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2007

Tip: Looking for answers? Try searching our database.

Extending the code

Thread view: 
Matt - 01 Jan 2007 19:48 GMT
I have the following code but i want to add a function which will allow
me to count the number of vowels in the text file. I was wondering if
anyone would be willing to give me some help with this

[CODE]
import java.io.*;
import java.util.*;
public class App {
//Create BufferedReader class instance
public static InputStreamReader input = new
InputStreamReader(System.in);
public static BufferedReader keyboardInput = new BufferedReader
(input);
public static void main(String[] args) {
 int[] lengths = new int[100]; //Runtime error if the file contains a
word of more than 100 characters
 int longestWord = 0;
        for(int i = 0; i < lengths.length; i++) {
  lengths[i] = 0;
 }
 try {
  System.out.println("Enter the name of the file you wish to read:
");
  String fileName = keyboardInput.readLine();
  FileReader file = new FileReader(new File(fileName));
  BufferedReader myFile = new BufferedReader(file);
  int numTokens = 0;
  int numWords = 0;
  int numChar= 0;
  String line;
  while ((line = myFile.readLine()) != null ) {
    String[] words = line.split(" ");
    numWords = numWords + words.length;
    for (int i = 0; i < words.length;i++) {
    numChar = numChar + words[i].length();
    if(words[i].length() > longestWord) {
     longestWord = words[i].length();
    }
    lengths[words[i].length()] = lengths[words[i].length()] + 1;
    }
  }
  System.out.println("\nThere are " + numWords + " words in the text
file");
  System.out.println("\nThere are " + numChar + " characters in the
text file");
  System.out.println("\nThere length frequency of the words is as
follows:");
  System.out.println("\nLength   :  Frequency ");
  for(int i = 0; i <= longestWord; i++) { //Print frequencies only up
to the longest word
    System.out.println(i + "    :"+lengths[i]);
  }
  myFile.close();
 }
 catch(IOException iO) {
  iO.printStackTrace();
 }
}
}
[/CODE]
John O'Conner - 01 Jan 2007 20:14 GMT
> I have the following code but i want to add a function which will allow
> me to count the number of vowels in the text file. I was wondering if
> anyone would be willing to give me some help with this

<snip>

>    while ((line = myFile.readLine()) != null ) {
>     String[] words = line.split(" ");
>     numWords = numWords + words.length;
>     for (int i = 0; i < words.length;i++) {
>     numChar = numChar + words[i].length();

So, why not count the vowels somewhere right about here? You can iterate
through the chars here, checking for a, e, i, o, and u and increment a
counter, right?

>     if(words[i].length() > longestWord) {
>      longestWord = words[i].length();
>     }
>     lengths[words[i].length()] = lengths[words[i].length()] + 1;
>     }
>    }

By the way, you or someone like you just recently posted a similar
question just days ago...maybe you should google the question and
responses?

I'm surprised by these questions though. They show a significant lack of
fundamental, core principals: loops, iterations, equality, counters. I
don't mean to sound critical, although I do know that this does...but
maybe you need to spend some time with the TA to get some basic things
resolved.

--
John O'Conner
Lew - 02 Jan 2007 06:39 GMT
> By the way, you or someone like you

with, coincidentally, the same name,

> just recently posted a similar question just days ago...
> maybe you should google the question and responses?

- Lew


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.