0

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 );
1stthomas
  • 731
  • 2
  • 15
  • 22
CoolHand
  • 65
  • 1
  • 5
  • Possible duplicate of [Java: How do I count the number of occurrences of a char in a String?](https://stackoverflow.com/questions/275944/java-how-do-i-count-the-number-of-occurrences-of-a-char-in-a-string) – 1stthomas Nov 23 '17 at 17:12
  • Thanks. I actually already reviewed this posting and it did not help as my problem has to fit a specific format and I believe the issue is related to the loop, not the counting of the characters hence the title. – CoolHand Nov 23 '17 at 17:17
  • Looks to me like the condition ought to be `startNxtSrch < length`. But I agree with some of the other comments: This is a very convoluted approach. And you probably should not add the `i` variable. I believe all the necessary variables are already present in the template. – Henrik Aasted Sørensen Nov 23 '17 at 17:20

4 Answers4

2

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 );
Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
0

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;
} 
Apostolis I.
  • 112
  • 6
  • I appreciate the feedback but the format has to follow what was provided and I can only change the ..... parts of the code. Seems overly complicated to me as well. – CoolHand Nov 23 '17 at 17:15
  • check my edited answer. i think that's what you are looking for :) – Apostolis I. Nov 23 '17 at 17:30
0

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);
Jimmy Fraiture
  • 370
  • 1
  • 2
  • 15
  • Totally agree. However, I am not looking for a "better way" as I am required to follow the format provided. It's overly complicated but I can only change the ...... parts of the format provided. – CoolHand Nov 23 '17 at 17:19
0

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 );
1stthomas
  • 731
  • 2
  • 15
  • 22
Safeer Ansari
  • 772
  • 4
  • 13
  • I agree, it's overly complicated but I am required to follow the format provided and only change the ..... parts of the code which is why I'm having so much trouble figuring it out. – CoolHand Nov 23 '17 at 17:18