G
George W. Cherry
I want to return three named ints from a method. I've done
it (as shown below) with a class that has three public int
fields. A pal of mine said this is execrable style. Is she
right? What would be the elegant received style of doing
what I want to do? (An old Ada programmer wants to know.)
George (Old Ada Programmer)
public class CountTheCharacters {
public static class NumberOf {
public int vowels;
public int consonants;
public int others;
}
public static NumberOf countTheCharacters(String str) {
NumberOf numberOf = new NumberOf();
for (int j = 0; j < str.length(); j = j + 1) {
char ch = str.charAt(j);
if ( Character.isLetter(ch) ) {
switch(ch) {
case 'a': case 'e': case 'i': case 'o': case 'u':
case 'A': case 'E': case 'I': case 'O': case 'U':
numberOf.vowels++;
break; //end cases for vowels
default:
numberOf.consonants++;
//end ch is a letter but not a vowel
}//end switch statement
}
else { //ch is not a letter
numberOf.others++;
}
}//end for loop
return numberOf;
}//end countTheCharacters method
}//end class
it (as shown below) with a class that has three public int
fields. A pal of mine said this is execrable style. Is she
right? What would be the elegant received style of doing
what I want to do? (An old Ada programmer wants to know.)
George (Old Ada Programmer)
public class CountTheCharacters {
public static class NumberOf {
public int vowels;
public int consonants;
public int others;
}
public static NumberOf countTheCharacters(String str) {
NumberOf numberOf = new NumberOf();
for (int j = 0; j < str.length(); j = j + 1) {
char ch = str.charAt(j);
if ( Character.isLetter(ch) ) {
switch(ch) {
case 'a': case 'e': case 'i': case 'o': case 'u':
case 'A': case 'E': case 'I': case 'O': case 'U':
numberOf.vowels++;
break; //end cases for vowels
default:
numberOf.consonants++;
//end ch is a letter but not a vowel
}//end switch statement
}
else { //ch is not a letter
numberOf.others++;
}
}//end for loop
return numberOf;
}//end countTheCharacters method
}//end class