Hi there,
I've been trying to find the errors of the following code and I'm totally stuck . The program it's the usual Vowel Counter.
If no sentence is been introduced then the program outputs the following sentence:
Usage: java VowelCounter [SENTENCE]
Try `java VowelCounter --help' for more information.
If only a word is introduced then the program outputs the following sentence:
Usage: java VowelCounter [SENTENCE]
Count the number of vowels in SENTENCE.
Example: java VowelCounter The quick brown fox jumps over the lazy dog
When we introduce more than one word then program counts the number or vowels.
I get the correct sentences when no introducing a word or only one word. When I introduce more than one word then the program does not count the vowels and I get the exception message.
PLEASE!!!! HEEELP!!! If someone could just guide me thru the error, so I can try to solve it...
ORIGINAL CODE
/** * Class that counts the number of vowels in a sentence. */ public class VowelCounter { /** Flag to show the help of this program. */ private static final String HELP_FLAG = "--help"; /** How to use this program. */ private static final String USAGE_LINE = "Usage: java VowelCounter [SENTENCE]"; /** The sentence which vowels are being counted. */ private String sentence; /** * Constructor. * @param words the words of the sentence which vowels will be counted */ public VowelCounter(final String[] words) { super(); String space; String phrase= ""; char x= 0; while (x < words.length) { if (x == 0) { space= ""; } else { space= " "; } phrase= phrase + space + words[x]; x= x + 1; } this.sentence= phrase; } /** * Getter. * @return the sentence which vowels are being counted */ public String getSentence() { return this.sentence; } /** * It counts the number of characters in the sentence. * @param c the character to count in the sentence * @return the occurrences of character 'c' in the sentence (case-insensitive) */ public int countNumberOf(char c) { int occurrences= 0; int x= 0; while (x <= this.getSentence().length()) { if (Character.toLowerCase(x) == Character.toLowerCase(this.getSentence().charAt(x))) { occurrences= occurrences + 1; } x= x + 1; } return occurrences; } /** * Main method. * @param args arguments */ public static void main(String[] args) { if (args.length() < 1) { System.out.println(USAGE_LINE); System.out.println("Try `java VowelCounter --help' for more information."); } else if (args.length == 1 && !HELP_FLAG.equals(args[0])) { System.out.println(USAGE_LINE); System.out.println("Count the number of vowels in SENTENCE."); System.out.println("Example: java VowelCounter The quick brown fox jumps over the lazy dog"); } else { try { VowelCounter vc= new VowelCounter(args[0]); System.out.println("The sentence '" + vc.getSentence() + "' has:"); System.out.println(vc.countNumberOf("a") + " 'A' vowels."); System.out.println(vc.countNumberOf('e') + " 'E' vowels."); System.out.println(vc.countNumberOf('i') + " 'I' vowels."); System.out.println(vc.countNumberOf('o') + " 'O' vowels."); System.out.println(vc.countNumberOf('u') + " 'U' vowels."); } catch (Exception ex) { System.out.println("Got error '" + ex.getMessage() + "'. Check you input and try again"); } } } }
MY CODE
/** * Class that counts the number of vowels in a sentence. */ public class VowelCounter { /** Flag to show the help of this program. */ private static final String HELP_FLAG = "--help"; /** How to use this program. */ private static final String USAGE_LINE = "Usage: java VowelCounter [SENTENCE]"; /** The sentence which vowels are being counted. */ private String sentence; /** * Constructor. * @param words the words of the sentence which vowels will be counted */ public VowelCounter(final String[] words) { super(); String space; String phrase= ""; int x= 0;//error while (x < words.length) { if (x == 0) { space= ""; } else { space= " "; } phrase= phrase + space + words[x]; x= x + 1; } this.sentence= phrase; } /** * Getter. * @return the sentence which vowels are being counted */ public String getSentence() { return this.sentence; } /** * It counts the number of characters in the sentence. * @param c the character to count in the sentence * @return the occurrences of character 'c' in the sentence (case-insensitive) */ public int countNumberOf(char c) { int occurrences= 0; int x= 0; while (x <= this.getSentence().length()) { if (Character.toLowerCase(x) == Character.toLowerCase(this.getSentence().charAt(x))) { occurrences= occurrences + 1; } x= x + 1; } return occurrences; } /** * Main method. * @param args arguments */ public static void main(String[] args) { if (args.length < 1) {//Error System.out.println(USAGE_LINE); System.out.println("Try `java VowelCounter --help' for more information."); } else if ((args.length == 1) && !(HELP_FLAG.equals(args[0]))) {//Error System.out.println(USAGE_LINE); System.out.println("Count the number of vowels in SENTENCE."); System.out.println("Example: java VowelCounter The quick brown fox jumps over the lazy dog"); } else { try { VowelCounter vc= new VowelCounter(args);//Error System.out.println("The sentence '" + vc.getSentence() + "' has:"); System.out.println(vc.countNumberOf('a') + " 'A' vowels.");//Error System.out.println(vc.countNumberOf('e') + " 'E' vowels."); System.out.println(vc.countNumberOf('i') + " 'I' vowels."); System.out.println(vc.countNumberOf('o') + " 'O' vowels."); System.out.println(vc.countNumberOf('u') + " 'U' vowels."); } catch (Exception ex) { System.out.println("Got error '" + ex.getMessage() + "'. Check you input and try again"); } } } }