0

I am writing a program that counts the amount of vowels in a string and if there are more vowels than consonants, it returns true. If not, false. This is a homework assignment, but the runner is not part of it. I want to test to see if my program works, which it should (hopefully!).

Now, for all our homeworks and labs, a runner is usually given. We were never taught how to write one, which is pretty bad since I'd like to check my code. I tried mimicking past runners, but I kept getting errors in my runner, some which read: "Cannot find symbol" How would I create a runner for this program?

Here is my code:

import static java.lang.System.*;
public class StringAnalyzer {
  //String word;
  public static boolean hasMoreVowelsThanConsonants(String word) {
    // String word = string.toUpperCase();
    int vowelCount;
    int newLength;
    for (vowelCount = 0; word.length() >= 1; vowelCount++) {

      if (word.indexOf("A") != 1) {
        vowelCount++;
      } else if (word.indexOf("E") != 1) {
        vowelCount++;
      } else if (word.indexOf("I") != 1) {
        vowelCount++;
      } else if (word.indexOf("O") != 1) {
        vowelCount++;
      } else if (word.indexOf("U") != 1) {
        vowelCount++;
      }


      newLength = (word.length() - vowelCount);

      if (vowelCount > newLength) {
        return true;
      } else {
        return false;
      }

    }
    return true;
  }
}

If you catch any problems, I'd always except advice :)

Here is my "runner" (its pretty bad, haha):

import static java.lang.System.*;

import static java.lang.System.*;


public class StringAnalyzerRunnerCDRunner {
  public static void main(String[] args) {
    hasMoreVowelsThanConsonants("DOG");
  }
}

Thank you :)

August
  • 12,410
  • 3
  • 35
  • 51
Nicole I.
  • 87
  • 1
  • 3
  • 10
  • I'm going to assume by runner, you mean program entry point / main method... Apart from the fact that you aren't printing anything in your main method (System.out.println(hasMoreVowelsThanConsonants("DOG"))), are you getting a specific error? – Adrian Leonhard Feb 25 '15 at 04:22
  • Read http://stackoverflow.com/a/19161184/1980909. Note that you can replace your second if clause with `return (vowelCount > newLength);` and that newLength should be called consonantCount. – Adrian Leonhard Feb 25 '15 at 04:26

2 Answers2

0

I'm not really sure what you're question is, but since you're asking for advice on the code you've written, heres my 2c.

Some tweaks :

public class StringAnalyzer {

    public static void main(String[] args) {

        String word;

        word = "Dog";
        System.out.println(word + " has more vowels than consonants? " + hasMoreVowelsThanConsonants(word));

        word = "Ace";
        System.out.println(word + " has more vowels than consonants? " + hasMoreVowelsThanConsonants(word));
    }

    public static boolean hasMoreVowelsThanConsonants(String word) {
        int vowelCount = 0;
        int consonantCount = 0;

        String[] split = word.split("");
        for(String s : split){
            if(s.toUpperCase().matches("A|E|I|O|U")){
                vowelCount++;
            } else {
                consonantCount++;
            }
        }

        return vowelCount > consonantCount;
    }
}

Some points I've changed:

  1. Removed unused System import, its not required
  2. Added main method, which is your entry point, or "runner" as you refer to it.
  3. Introduced split word.split("");, this will give you an array of characters from the String, it's a bit nicer to work with.
  4. Convert each character to upper case, then compare it to a regex, meaning either A,E,I,O or U.
  5. Kept a count of consonants/vowels, the return just compares if vowels are greater, and returns true if so.

Theres many ways to do this. You might like to consider what happens if the counts are equal...or if the word is null, protect from null pointers etc, but that's down to you ;)

Jimmy
  • 16,123
  • 39
  • 133
  • 213
0

First of all, the error message you received error: cannot find symbol is either: the packaged class has not been imported, and/or you have misspelled the variable, class or method name

Another approach:

You can name your main class as such from public class StringAnalyzer{ to public class Main { or class StringAnalyzer

I did not want to stray too far away from what you have but here is a snippet of another approach:

import java.lang.*;
import java.util.Scanner;

class StringAnalyzer
{

   public static void main(String args[])
   {
      Scanner in = new Scanner(System.in);
      String sampleword = in.nextLine();
      System.out.println("You entered string: "+sampleword);
      System.out.println("Is your vowels more than the consonants: "+ hasMoreVowelsThanConsonants(sampleword));
   }

    private static boolean hasMoreVowelsThanConsonants(String word) {
        int vowelCount = 0;
        int newLength = 0;

        for (int i = 0; i < word.length(); i++) {
          if (word.charAt(i) == 'A') {
            vowelCount++;
          } else if (word.charAt(i) == 'E') {
            vowelCount++;
          } else if (word.charAt(i) == 'I') {
            vowelCount++;
          } else if (word.charAt(i) == 'O') {
            vowelCount++;
          } else if (word.charAt(i) == 'U') {
            vowelCount++;
          }
        }

        newLength = word.length() - vowelCount;

        return vowelCount > newLength;
      }
   }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
André.
  • 1
  • 2