简体   繁体   中英

I need to count the total amount of words, average number of words per line and vowels per line of a text document

I'm relatively new to java and have a problem with a program I'm working on. The main idea of the program is to be given a document an calculate the total amount of words in the document, average number of words per line and vowels per line. I've come up with a starting point but don't really know how to continue.

import java.io.*;

public class Example {
    public static void main(String[] args) throws IOException {
        int vowelCount = 0, wordsAvg = 0, wordsCount = 0, a;
        char ch;
        String line;
        int vowels1 = 0, vowels2 = 0, vowels3 = 0, vowels4 = 0, vowels5 = 0, vowels6 = 0, vowels7 = 0, vowels8 = 0, vowels9 = 0, vowels10 = 0;
        BufferedReader in ;

        in = new BufferedReader(new FileReader("message.txt"));

        line = in .readLine();
    }
    for (int i = 0; i < line.length(); i++) {
        ch = line.charAt(i);

        if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') vowels1++;
    }

    {
        System.out.println(line);
        System.out.println("--------------------------------------------");
        System.out.println("Total number of words: " + wordsCount);
        System.out.println("Average number of words per line: " + wordsAvg);
        System.out.println("The number of vowels in line 1 is: " + vowels1);
        System.out.println("The number of vowels in line 2 is: " + vowels2);
        System.out.println("The number of vowels in line 3 is: " + vowels3);
        System.out.println("The number of vowels in line 4 is: " + vowels4);
        System.out.println("The number of vowels in line 5 is: " + vowels5);
        System.out.println("The number of vowels in line 6 is: " + vowels6);
        System.out.println("The number of vowels in line 7 is: " + vowels7);
        System.out.println("The number of vowels in line 8 is: " + vowels8);
        System.out.println("The number of vowels in line 9 is: " + vowels9);
        System.out.println("The number of vowels in line 10 is: " + vowels10);
        line = in .readLine();
    }
}

So far I have the program reading the file and counting the vowels for the first line but I dont really know how to loop through each other line. Also I need the output to show as I have it pre-setup. Any help is appreciated, trying to keep it as simple as possible would be great.

You may read a file in java using BufferedReader until the line is not null. While reading a file you may call appropriate method to calculate number of word, vowel etc -

BufferedReader br = new BufferedReader(new FileReader("file.txt"));
    try {

        String line = br.readLine();

        while (line != null) {
            int wordsPerLine = countWord(line);
            int vowelsPerLine = countVowel(line);

            line = br.readLine();
        }

    } finally {
        br.close();
    }  

Now you can write your countWord(String line) method like this -

//this method will count word for per input line
public int countWords(String line)
{
   String words[]=line.split(" ");
   int count=words.length;
   return count;
}  

And you may also count vowels per line like this -

public int coutnVowels(String line){

   int count =0;
   for(int i=0; i<line.length; i++){

      char currentChar = line.CharAt(i);
      if(currentChar=='a'||currentChar=='e'||currentChar=='i'||currentChar=='o'
      currentChar=='u'){

         count++;
      }
   }

   return count;
}

Looping through the file

BufferedReader br;
String line = br.readLine();  
while (line != null)  
{  
   ... // do stuff to file here  
   line = br.readLine();  
} 

EDITED

Scanner myScanner = new Scanner(new File(fileName));
int nbLines = 0, nbWords = 0, nbVowels = 0;

while(myScanner.hasNextLine()) {
line = myScannner.nextLine();
    nbWords += countWords(line); // the method in Razib's post
    ++nblines;  // increment number of lines
    nbVowels += coutnVowels(line); // again use the method Razib gave you.
}
myScanner.close();

//nbWords now contains the number of words in the file.
// nblines is the number of line in the file.
// and nbVowels the total number of vowels in the file.

int avrg = nbWords / nblines; //average words per line.

this should work.

you should be able to do the rest yourself ;)

Another tip is to use the right datatype to store the vowels for each line.

Since we do not know how many lines there will be in the file an ArrayList would be useful:

import java.util.ArrayList;

ArrayList<Integer> vowels = new ArrayList<Integer>()

while (line != null) {


    vowels.add(countVowels(line));

} 

This will store the vowels for each line in the array list.

then to display the counts you loop over the vowels array list, loop over the arraylist:

int lineNo = 1;
for(Integer nrVowels : vowels) {
    System.out.println("The number of vowels in line " 
                       + lineNo + " is: "+ nrVowels);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM