HI, so my code compiles but does not give the result I intended. Any help is greatly appreciated!
How can we count the number of occurrences of a character or group of characters (a substring) inside of a given string of characters? In other words, how can we count the number of time a certain section of text appears in another section of text?

Your assignment is to write a SubstringCounter class with only one method:

public static int substringCounter(String string, String substring)

The method will utilize two strings as passed parameters. The first string is the master string to be searched, and the second string is the string being searched for. The method returns the count (an integer) of the number of times the substring is found in the master string. You should assume that either the master string or the substring is one or more characters, and that either one may be longer than the other. The substring (obviously) may occur one or more times in the master string, or possibly not at all. You will want to have some kind of process that will look at each of the characters in the master string and count every time the substring is found.

If you are not sure exactly how you want to do this, you will want to review the advanced operations on strings section of the textbook, and in particular the string method descriptions listed on pages 265-6. You will also want to look at the IndexOfExample and SubstringExample classes that are provided for you in the project download.

To get started, download, extract, and save the Substring Counter project to your computer. Open up the project by clicking on the BlueJ package file icon. You can run the SubstringCounterTest class anytime you like to see how much of the assignment you have completed (go ahead, and give it a try right now). When you have written your method, run the tester and see if your code passes the 10 rigorous tests.

public class SubstringCounter
{
    public static int substringCounter(String master,  String substring)
    { int count = 0 ;
        for(int pos = 0 ; pos < master.length() ; pos++)
        {
            int index = master.indexOf(substring, pos) ; 
            if(index!=0){
                pos = count ; 
                count++ ; 
            }
        }
        return count ;
    }
}

This is what the tester says:
Now testing your SubstringCounter class:

Found method substringCounter(String string, String substring)
Failed: substring 'the' does not appear 178 times in the string
'Fourscore and seven years ago, our fathers brought forth upon this continent
a new nation, conceived in liberty and dedicated to the proposition
that all men are created equal.'
Failed: substring 'and' does not appear 178 times in the string
'Fourscore and seven years ago, our fathers brought forth upon this continent
a new nation, conceived in liberty and dedicated to the proposition
that all men are created equal.'
Failed: substring 'e' does not appear 178 times in the string
'Fourscore and seven years ago, our fathers brought forth upon this continent
a new nation, conceived in liberty and dedicated to the proposition
that all men are created equal.'
Failed: substring ' ' does not appear 178 times in the string
'Fourscore and seven years ago, our fathers brought forth upon this continent
a new nation, conceived in liberty and dedicated to the proposition
that all men are created equal.'
Failed: substring '.' does not appear 178 times in the string
'Fourscore and seven years ago, our fathers brought forth upon this continent
a new nation, conceived in liberty and dedicated to the proposition
that all men are created equal.'
Failed: substring 'F' does not appear 178 times in the string
'Fourscore and seven years ago, our fathers brought forth upon this continent
a new nation, conceived in liberty and dedicated to the proposition
that all men are created equal.'
Failed: substring 'aa' does not appear 5 times in the string
'aaaaa'
Failed: substring 'googoo' does not appear 18 times in the string
'googoogoogoogoogoo'
Failed: substring 'beanwhip' does not appear 12 times in the string
'doobydoobydo'
Failed: substring 'biggerstring' does not appear 6 times in the string
'string'
Bummer, try again

Recommended Answers

All 4 Replies

It is updated because I had to include a descprition of project please delete that one.

I noticed a few things with your code:

you're using 2 variables,index and pos when 1 will do, just give it the new value from indexOf

you're using a for loop and changing it's counter. This is bad form and can lead to hard to find bugs in your code. A while loop works better when you need to change the loop counter by different amounts.

you're updating pos with count. Basically this increments pos by 1 each time, so indexOf is re-reading the same substring over and over again.

You're checking if index equals 0, when the no match return value is -1

Here's one way it could be done:

public static int substringCounter(String master,  String substring)
{ 
    int count = 0 ;
    int index = 0;
    while(index < master.length() && index != -1)
    {
        index = master.indexOf(substring, index);
        if(index != -1)
        {
            count++;
            //set index to 1 past the start of the substring, so that indexOf doesn't re-read it.
            index++;
        }
    }
    return count ;
}

A good example of why reading the API doc is never a waste of time!
The behaviour of indexOf for index >= length is defined in the API JavaDoc (it returns -1), so that code can be simpler, eg

count = 0;
index = 0;
while((index = master.indexOf(substring, index)) != -1) {
   count++;
   index++;
}

or, if the assignment in the while seems confusing...

count = 0;
index = master.indexOf(substring); // first occurrence
while (index != -1) {
   count++;
   index = master.indexOf(substring, ++index); // next one
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.