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 );
3
  • Possible duplicate of Java: How do I count the number of occurrences of a char in a String? Commented Nov 23, 2017 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. Commented Nov 23, 2017 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. Commented Nov 23, 2017 at 17:20

4 Answers 4

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 );
Sign up to request clarification or add additional context in comments.

Comments

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);

1 Comment

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.
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;
} 

2 Comments

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.
check my edited answer. i think that's what you are looking for :)
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 );

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.