Reputation: 75
I have this code where I want to add the value 97 for a char variable, but instead to obtain, according with ASCII table, the final number 98 for the letter 'b' the output is 205.
What is wrong in my code?
public class MyTest {
public static void main(String[] args) {
char result;
String input = "blablab";
result = charCounter(input);
}
private static char charCounter(String input) {
int[] array = new int[26];
for (int i = 0; i < input.length(); i++) {
char ch = input.charAt(i);
array[ch - 97]++;
}
int max = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
char frequent = input.charAt(i);
System.out.println(frequent + 97);
}
}
return 45;
}
}
Thank you for your help.
Upvotes: 0
Views: 1885
Reputation: 2329
replace
System.out.println(frequent + 97);
with
System.out.println((char)(i + 97));
because
char + Integer = Integer
so you have to convert it to char
and the ascii
of corresponding char should be
i + 97
rather than
frequent + 97
Upvotes: 1