Hrushikesh Salunkhe
Hrushikesh Salunkhe

Reputation: 81

String indexOf method is not working for first character in the String

I am implementing a code for counting number of occurrences for all the characters in a String. I have used indexOf() method to check occurrences. But it's not working for the first character. The following code works fine for all characters except the first character.

public static void main(String[] args) {
        String str = "simultaneously";
        int[] count = new int[str.length()]; //counter array

        for (int i = 0; i < count.length; i++) { //initializing counters
            count[i] = 0;
        }

        for (int i = 0; i < str.length(); i++) {
            int index = -1;
            char c = str.charAt(i);
            while (1 > 0) {

                if (str.indexOf(c,index) > 0) {  //This is not working for 
                                                 //the first characters
                    index = str.indexOf(c, index) + 1;
                    count[i]++;
                } else {
                    break;
                }
            }
        }

        for (int i = 0; i < count.length; i++) {
            System.out.println(str.charAt(i) + " occurs " + count[i] + " times");
        }
    }

Upvotes: 2

Views: 1746

Answers (4)

Eduardo Morales
Eduardo Morales

Reputation: 843

The index of arrays and Strings (which are array of characters) always start at 0 in Java.

You also want to check position 0, so include >=.

       if (str.indexOf(c,index) >= 0) {

Also, using breaks can sometimes be confusing. In your code, your while-loop is an infinite True and then you break out of it when necessary.

Take a look at this code below. It serves the same purpose that you want to accomplish. It is much cleaner and clearer as it removes the break from the whileloop and you simply check to see if the statement is True at the beginning of the while-loop rather than inside it.

        for (int i = 0; i < str.length(); i++) {
            int index = 0;
            char c = str.charAt(i);
            while (str.indexOf(c,index) >= 0) {
                index = str.indexOf(c, index) + 1;
                count[i]++;
            }
        }

Upvotes: 2

Zain Ul Abidin
Zain Ul Abidin

Reputation: 2710

There is some thing else you need to know

str.indexOf(c,index)

will search for the character c from the index 'index' which is -1 in your case for the first character that is it will never find this because starting point of string is 0 also change your condition as following

str.indexOf(c,index) >= 0

Upvotes: 2

chaitanya89
chaitanya89

Reputation: 847

Index for arrays in java starts from 0. Change the condition from

if (str.indexOf(c,index) > 0) {

to

if (str.indexOf(c,index) >= 0) {

And also, the for-loop to initialize the counter is redundant, by default, all the values in the int array is initialized to 0.

Upvotes: 9

Eran
Eran

Reputation: 393936

str.indexOf(c,index) would return 0 for the first character, but your condition checks whether str.indexOf(c,index) > 0. Change it to str.indexOf(c,index) >= 0.

Upvotes: 7

Related Questions