I've made code that scores each letter in a word you input according to the table below:
1: AEILNORSTU
2: DG
3: BCMP
4: FHVWY
5: K
8: JX
10: QZ
I appreciate any feedback, but the main reason of this post concerns the charPoint
method. Should I rewrite this using switch
? Or is there an even better way to write it?
package exercicios;
import com.sun.org.apache.xalan.internal.xsltc.compiler.Pattern;
import acm.program.*;
public class Ex5 extends ConsoleProgram{
public void run(){
String word = readLine("Enter a word: ");
println(wordScore(word));
}
private int wordScore(String word){
int score = 0;
for (int i = 0; i < word.length(); i++){
score += charPoint(word.substring(i, i+1));
}
return score;
}
private int charPoint(String ch){
if (ch.matches("[AEILNORSTU]")){
return 1;
} else if (ch.matches("[DG]")){
return 2;
} else if (ch.matches("[BCMP]")){
return 3;
} else if (ch.matches("[FHVWY]")){
return 4;
} else if (ch.matches("[K]")){
return 5;
} else if (ch.matches("[JX]")){
return 8;
} else if (ch.matches("[QZ]")){
return 10;
} else return 0;
}
}
if..else
orswitch
at all. \$\endgroup\$