Auzzie2009
Auzzie2009

Reputation: 11

Adding an exception for a letter

In my program it searches a text file, in this case a list of every word in a dictionary, and adds a number to the count of vowels if aeiou are found in it. I need to add an exception for counting y, it needs to only be counted if none of the other vowels are found in the word. I am fairly new to the concept and wondering if I was going in the right direction. And help would be loved!

package TEST;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class test {

    public static void main(String[] args) throws FileNotFoundException, IOException{
        int count= 0;

        FileReader FR = new FileReader("Words.txt");
        BufferedReader BR = new BufferedReader(FR); 

        String Vowels;
        while((Vowels= BR.readLine()) != null) {

                for (int i = 0; i < Vowels.length(); i++) {
                  char c = Vowels.charAt(i);

                if (c=='a'||c=='e'||c=='i'||c=='o'||c=='u' ) {
                       count++;
                 // if (c=='y') {
                 //    count++;

                           }}}
              System.out.println("Total:"+ count);
                }}

Upvotes: 0

Views: 98

Answers (3)

davidxxx
davidxxx

Reputation: 131396

To be sure that y is not contained in the word, you have to wait that all the letters of the words were read. So you should increment the number of y found for the current word after the for loop if it is relevant, that is if any a-e-i-o-u letters were found.

The variables you will need :

  • int countY to count the number of y found in the current word.
  • boolean isOtherVoyelsThanYfound to flag if any a-e-i-o-u letters were found in the current word.

These should be reinit to 0 and false for each new word to analyse.

Here is the idea :

while{
...
  boolean isOtherVoyelsThanYfound = false;
  int countY = 0;

  for (int i = 0; i < Vowels.length(); i++) {
    char c = Vowels.charAt(i);

    if (c=='a'||c=='e'||c=='i'||c=='o'||c=='u' ) {
         count++;
         isOtherVoyelsThanYfound = true;
    }
    else if (c == 'y'){
        countY++;
    }
  }

  if (!isOtherVoyelsThanYfound){
    count += countY;
  }
... 
}

Upvotes: 2

Joshua Latusia
Joshua Latusia

Reputation: 11

Hey you can use another int to count every "y" and count them. Then in the end you can check if count != 0 or else overwrite it with your yCount. So your code should look something like this.

public static void main(String[] args) throws FileNotFoundException, IOException{
        int count= 0;
        int yCount = 0;

    FileReader FR = new FileReader("Words.txt");
    BufferedReader BR = new BufferedReader(FR); 

    String Vowels;
    while((Vowels= BR.readLine()) != null) 
    {

            for (int i = 0; i < Vowels.length(); i++) 
            {
              char c = Vowels.charAt(i);

              if (c=='a'||c=='e'||c=='i'||c=='o'||c=='u')
                   count++;

              if (c=='y') 
                   yCount++;

             }

             if(count == 0)
             count = yCount;

          System.out.println("Total:"+ count);
  }

This way you dont have to scan the file twice when count == 0.

Upvotes: -1

thyago stall
thyago stall

Reputation: 1704

You can try this approach:

while((Vowels= BR.readLine()) != null) {
    boolean foundVowel = false;    

    for (int i = 0; i < Vowels.length(); i++) {
        char c = Vowels.charAt(i);

        if (c=='a'||c=='e'||c=='i'||c=='o'||c=='u') {
            count++;
            foundVowel = true;
        }
    }

    if (!foundVowel) {
        for (int i = 0; i < Vowels.length(); i++) {
            char c = Vowels.charAt(i);

            if (c=='y'||c=='e'||c=='i'||c=='o'||c=='u') {
                count++;
            }
        }        
    }
}
System.out.println("Total:"+ count);

Here you scan all the word and when you don't find any vowel, you scan it again just searching ys.

Upvotes: 1

Related Questions