I am new to javaand programming. I gave the task to count the appearance of a unique symbol. I should use only an array. I have the current code -
public class InsertChar{
public static void main(String[] args){
int[] charArray = new int[1000];
char[] testCharArray = {'a', 'b', 'c', 'x', 'a', 'd', 'c', 'x', 'a', 'd', 'a'};
for(int each : testCharArray){
if(charArray[each]==0){
charArray[each]=1;
}else{
++charArray[each];
}
}
for(int i=0; i<1000; i++){
if(charArray[i]!=0){
System.out.println( i +": "+ charArray[i]);
}
}
}
}
For the testCharArrayexit should be -
a: 4
b: 1
c: 2
d: 2
x: 2
But it gives me the next way out -
97: 4
98: 1
99: 2
100: 2
120: 2
How can i fix this?
source
share