1

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 !

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
machekj
  • 65
  • 8
  • Why? This is a job for a map, not an array. – Dave Newton Oct 22 '13 at 14:37
  • i bet everything that i have, that there is already a solution for this outthere – Philipp Sander Oct 22 '13 at 14:40
  • 3
    Of course there are already solutions. This is an extremely common beginner assignment. See: http://stackoverflow.com/questions/6712587/counting-frequency-of-characters-in-a-string/6712620 – Zong Oct 22 '13 at 14:41
  • well im on the array chapter, guess it should be done with arrays – machekj Oct 22 '13 at 14:42
  • then throw away the book you are reading... – Philipp Sander Oct 22 '13 at 14:43
  • What exactly is the problem you're trying to solve? Are you just saying that you want the array to be `trimmed` down to size, leaving out the null indices? – Paul Samsotha Oct 22 '13 at 14:43
  • thats exactly what im trying to do – machekj Oct 22 '13 at 14:45
  • Your question may or may not have a unique element. Could you give us an example of the correct output for the example? I'm assuming you want to find the frequencies while printing out unique characters in preserved order. – Zong Oct 22 '13 at 14:46
  • Just create another array using a loop, and only insert the values that are not 0 – Paul Samsotha Oct 22 '13 at 14:46
  • No correct output avaible, reading lines from a file, comparing them with the string and should output the words that can be formed by the entered string from the console. I guess peeskillet proposal will do the work, i just thought there's another way to do it – machekj Oct 22 '13 at 14:48

2 Answers2

0

To trim the the array, create a new array based off the number of chars with values > 0

int charsWithValuesCount = 0;
for (int i = 0; i < charsCount.length; i++) {
    if (charsCount[i] > 0) {
        charsWithValuesCount++;
    }
}

char[] newCharArray = new char[charsWithValuesCount];

// next: 
// loop to give array values of your characters
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
0

Simply you can use another index variable for your countedChars and counts arrays. Lets say b. And at the end of the for loop check if b < wordCount.length and assign the remaining characters NULL and 0.

Or after the for loop just create a new array with the lenght of your valid character count and copy the valid characters and their counts to the new array.

kerberos84
  • 300
  • 1
  • 10