VowelCounter
Publicado por Liki (3 intervenciones) el 26/09/2012 11:14:27
Muy buenas,
Después de un par de días dándole al tarro con el siguiente programa, no logro ver dónde está el error. Creo que ya estoy viciado y no consigo ver dónde falla. Alguien que me pueda echar un lazo por favor?
Se trata del típico contador de vocales. Si no se introduce ninguna palabra aparece por pantalla un mensaje. Si sólo se introduce una palabra aparece otra frase y cuando se introducen más de una palabra debería contar el número de vocales de dicha frase.
Después de un par de días dándole al tarro con el siguiente programa, no logro ver dónde está el error. Creo que ya estoy viciado y no consigo ver dónde falla. Alguien que me pueda echar un lazo por favor?
Se trata del típico contador de vocales. Si no se introduce ninguna palabra aparece por pantalla un mensaje. Si sólo se introduce una palabra aparece otra frase y cuando se introducen más de una palabra debería contar el número de vocales de dicha frase.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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;
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 x;
}
/**
* 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==(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);
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");
}
}
}
}
Valora esta pregunta


0