1

can you help me why I got proper output though i have provided many spaces, this program is just counting the one space in if/ else condition

public class CountingtheCharactersinString
{
    public static void main(String[] args) 
    {
      String name = "This           is              my  first demo";
      int count = 0;
      boolean b = true;
      for(int i =0; i<name.length(); i++)
      {
          if(name.charAt(i)==' ')   b = true;
          else if(b==true)
          {
              b= false;
              count = count+1;
          }
      }
        System.out.println(count);
    }
}
3
  • 1
    so whats the error you are having? Commented Sep 3, 2021 at 2:20
  • Can you please what is your expected and actual out put to give more clarity Commented Sep 3, 2021 at 2:22
  • I am confused why it is ignoring multiple spaces Commented Sep 3, 2021 at 4:14

2 Answers 2

1

Answer as I understand the Question
Because you didn't provide much information. I think you are not getting the correct count value as you expected. The reason you're getting the wrong answer is that you checking the weather b==true and increase the counter. so, you will only get the count of first letters in the words of your string.

so to get the count, new main method

public static void main(String []args){
        String name = "This           is              my  first demo";
        int count = 0;
        boolean b=true;
        for(char c : name.toCharArray()){
            if (c==' '){
                b=true;
            }else{
                b=false;
                count+=1;
            }
        }
        System.out.println(count);
}

Here is what I have done was convert the string into a character array and loop through it and assign each value to the char c variable and check whether the value is ' '(space), if not I increase the count by one and there is no need to specify a boolean b. Because it won't do anything .
Without boolean b code get much more simpler,

public static void main(String []args){
        String name = "This           is              my  first demo";
        int count = 0;
        for(char c : name.toCharArray()){
            if (c!=' '){
                count+=1;
            }
        }
        System.out.println(count);
}

now I just check whether the value of char c not a ' '(space) and increase the count by one.

Sign up to request clarification or add additional context in comments.

1 Comment

no need to specify a boolean b there is no use for the code - that's for sure
0

This code isn't counting spaces, it's counting "words" by looking at non-space characters that either are at the start of the string or follow a space. Multiple spaces are ignored.

When I run the program, I get 5 as the output. I added logging before each time the count is incremented:

System.out.println("Incrementing at " + i);
count = count+1;

This results in this output:

Incrementing at 0
Incrementing at 15
Incrementing at 31
Incrementing at 35
Incrementing at 41

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.