You should simply check if val
's going to be out of range in your getFreq
function.
You probably also need to check if there's actually an element at the target index (i.e. it's not null
).
Also, as pointed out in the other answer, you're passing an 'invalid' string to your function, because 1
results in a negative val
value / index - either you should avoid doing this, or you could add that check to your function as well.
public int getFreq(String word) {
int val = word.charAt(0) - 64;
if (val < 0 || val >= c.length || c[val] == null)
return -1;
...
}