CoolHand
CoolHand

Reputation: 65

Trouble with Loop to Count Number of Given Characters in a String

I have to use a provided loop to count the number of times the character 'b' appears in a String fourthString and for some reason it's returning an incorrect number. I believe it has to do with the if condition but I could be mistaken.

Any help would be HUGELY appreciated.

The string:

String fourthString = "a bat for baseball";

It should return 3.

The format for the code:

char target        ='b';                               
int  length        = fourthString.length( );               
int  count         = 0;                                
int  startNxtSrch  = 0;                                
int  foundAt       = 0;                               

while( ....... ) {
    foundAt = .......;
    if ( ....... ) {
        startNxtSrch = foundAt +1;
        count += 1;
    } else {
        startNxtSrch = length;
    }
}  
System.out.printf( "%nThere are %d occurrences of '%c' in fourthStr.%n", 
                   count,
                   target );

What I tried that is returning an incorrect number:

int i = 0;

while( i < length ) {
    foundAt = startNxtSrch;
    if ( fourthString.indexOf('b', startNxtSrch) != -1 ) {
        startNxtSrch = foundAt + 1;
        count += 1;
        i++;
    } else {
        startNxtSrch = length;
        i++;
    }
}
System.out.printf( "%nThere are %d occurrences of '%c' in fourthStr.%n", 
                   count,
                   target );

Upvotes: 0

Views: 53

Answers (4)

Safayat Rahaman
Safayat Rahaman

Reputation: 66

This will give you the correct count of chars:

char target        = 'b';
int  length        = fourthString.length( );
int  count         = 0;
int  startNxtSrch  = 0;
int  foundAt       = 0;

while(startNxtSrch < length) {
    foundAt = fourthString.indexOf(target,startNxtSrch);
    if (foundAt>=0) {
        startNxtSrch = foundAt + 1;
        count += 1;
    } else {
        startNxtSrch = length;
    }
}
System.out.printf( "%nThere are %d occurrences of '%c' in fourthStr.%n",
            count,
            target );

Upvotes: 2

Safeer Ansari
Safeer Ansari

Reputation: 790

This, to me looks very complicated.

If I would write this code, here is a simpler solution:

while( i < length ) {
    if (fourthString.contains("b")) {
        fourthString = fourthString.replaceFirst("b", "");
        count++;
    }
    i++;
}

System.out.printf( "%nThere are %d occurrences of '%c' in fourthStr.%n",
                count,
                target );

Upvotes: 0

Apostolis I.
Apostolis I.

Reputation: 112

You can use the following line of code:

int count = StringUtils.countMatches("the string you want to search in", "desired string");

For your example:

int count = StringUtils.countMatches("a bat for baseball", "b");

You can find a similar question and this answer here: https://stackoverflow.com/a/1816989/8434076

EDIT

while(startNxtSrch != lenth)
{
   foundAt = indexOf('b', startNxtSrch);
   if (foundAt != -1)
   {
      startNxtSrch = foundAt +1;
      count += 1;
   }  
   else
      startNxtSrch = length;
} 

Upvotes: 0

Jimmy Fraiture
Jimmy Fraiture

Reputation: 452

A better way to do it is to pass throught the whole array and count the number of time your char occured.

String fourthString = "a bat for baseball";
char target = 'b';
count = 0;
for(int i = 0; i < fourthString.length; i++){
    if(fourthString.charAt(i) == traget){
        count++;
    }
}
System.out.println(count);

Upvotes: 0

Related Questions