I'm trying to read a string from console, turn it to a char array, then count each char and
its occurencies and then put it in a new char array.
public static void main(String... args) throws FileNotFoundException {
File words = new File("words.txt");
Scanner input = new Scanner(System.in);
Scanner fileInput = new Scanner(words);
System.out.println("Enter a word : ");
String word = input.nextLine().toLowerCase();
char[] wordCount = word.toCharArray();
char[] countedChars = new char[wordCount.length];
int[] counts = new int[countedChars.length];
for(int a=0; a<wordCount.length; a++) {
Character currentValue = wordCount[a];
int count = 1;
if(currentValue != null ) {
for(int j = a+1; j<wordCount.length; j++) {
if(wordCount[j] == currentValue) {
wordCount[j] = 0;
count++;
}
}
}
if(currentValue != 0) {
countedChars[a] = currentValue;
counts[a] = count;
}
}
Thing is when i create the countedChars array i do it with wordCount.length and this leaves me with empty space in my new array and i can't use to correctly compare later on in my program.It's ok if all chars occur just once, but if it's more than once i get the empty slots and this particular output with word - SWIMMING
s - 1
w - 1
i - 2
m - 2
- 0
- 0
n - 1
g - 1
I'd like to do it using arrays, not arraylists or anything else.Thanks for your time !