Hello, I am working on a program that requires the splitting of a string (the string in question is "BSHDDBDSBSHHSBH") so that it can count the occurrences of each separate letter. The code I currently have is:
public static void countPercent(Scanner input) {
String nucl = input.next();
int[] count = new int[4];
for (int i = 0; i < nucl.length(); i++) {
char single = nucl.charAt(i);
if (single == 'B') {
count[0]++;
} else if (single == 'D') {
count[1]++;
} else if (single == 'H') {
count[2]++;
} else {
count[3]++;
}
}
}
The expected output should be in an array like this: [3, 3, 5, 7] for example. Currently, I get one number in the final cell, but all the rest read zero. I have spent so long trying to figure this one out!
Thanks in advance!