How could I assign a int value to each of the sublists in the multidimensional char array
public int getScore(String Input)
{
/* Initial score is 0 but then will increase based on letter value*/
int totalScore = 0;
/* Takes countryInput value and converts it to a character array*/
char[] word = Input.toCharArray();
/* Grouped letters together based on point values */
char[][] letters = {{'a','e','i','o','u','l','n','s','t','r'},{'d','g'},
{'b','c','m','p'},{'f','h','v','w','y'},{'k'},{'j','x'},{'q','z'}};
return totalScore;
}
As mentioned in the comment, if you are flexible in storing char[][] letters in Map, one of the ways could be -
public int getScore(String input)
{
/* Initial score is 0 but then will increase based on letter value*/
int totalScore = 0;
/* Takes countryInput value and converts it to a character array*/
char[] word = input.toLowerCase().toCharArray();
/* Grouped letters together based on point values */
//char[][] letters = {{'a','e','i','o','u','l','n','s','t','r'},{'d','g'},
// {'b','c','m','p'},{'f','h','v','w','y'},{'k'},{'j','x'},{'q','z'}};
/*Store letters grouping in map with their value as Key*/
Map<Integer, List<Character>> letters = new HashMap<>();
letters.put(1, Arrays.asList('a','e','i','o','u','l','n','s','t','r'));
letters.put(2, Arrays.asList('d','g'));
letters.put(3, Arrays.asList('b','c','m','p'));
letters.put(4, Arrays.asList('f','h','v','w','y'));
letters.put(5, Arrays.asList('k'));
letters.put(6, Arrays.asList('j','x'));
letters.put(7, Arrays.asList('q','z'));
for(char ch: word) {
for(Entry<Integer, List<Character>> entrySet : letters.entrySet()) {
if(entrySet.getValue().contains(ch)) {
totalScore += entrySet.getKey();
break;
}
}
}
return totalScore;
}
Other way round, you can store the letters as key and their point value as value in map like this -
Map<Character, Integer> map = new HashMap<>();
map.put('a',1);
.
.
.
map.put('z',7);
then you logic will be simplified a lot -
for(char ch: word) {
totalScore += map.get(char);
}
and you are done.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.