0

I'm trying to count the number of non-blank characters in a string.

It works fine when there are no leading blank spaces, but when I add 3 spaces in from, it doubles the number of non-blank characters.

This is my code:

import java.io.*;
import java.util.*;

public class countCharacters
{

public static void main(String[] args) throws Exception
{
  String str1;
  int count;
  count = 0;



  BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a string: ");
  str1 = dataIn.readLine();
while(str1.length() > 0)
      {


  System.out.println("The String ''" + str1 + "''");
  System.out.println("has " + str1.length() + " Characters, including all blanks.");
  for(int i=0; i < str1.length(); ++i)
    if(str1.charAt(i) !=' ')
    count++;
  str1 = str1.trim();
   System.out.println("and " + str1.length() + " Characters, trimmed of leading and trailing blanks.");
   System.out.println("and " + count + " non-blank characters.");
System.out.println("");

System.out.print("Enter a string: ");
  str1 = dataIn.readLine();
   }

  System.out.println("Program complete.");

}
}
5
  • Trust me - formatting your code will help you a LOT in the debugging process. How can you check your code if you can't read it! If you're using Eclipse, hit control+shift+f, and your code will be autoformatted. I honestly can't look at your code at this point because it isn't readable to me. Commented Nov 4, 2014 at 18:30
  • Isn't it better if you trim before counting the characters? Commented Nov 4, 2014 at 18:30
  • @MatiCicero He/she is only doing trim in order to output the trimmed length of the string. The characters have already been counted by then. It doesn't matter whether they're counted before or after the trim. Commented Nov 4, 2014 at 18:31
  • But if you are not counting (and omitting) white space characters, trimming before counting will be a performance improvement, since you are removing characters you know you will not count Commented Nov 4, 2014 at 18:32
  • That's true, but this question isn't about performance. Commented Nov 4, 2014 at 18:33

4 Answers 4

1

Are you sure that it doubles the count every time? Maybe this only happens on the second time through the main loop?

You should be resetting count when you enter a new string. Otherwise, you're just adding to the count from the previous time through the main loop. Add a line like count = 0; before the System.out.print("Enter a string: "); at the bottom of the main loop, or declare and initialise count inside the loop, rather than before the loop.

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

Comments

0

A much cleaner way to do this would be to just make a copy without any spaces and compare the lengths:

    String str1 = "  the quick brown fox  ";
    String spaceless = str1.replace(" ", "");
    System.out.println("Number of spaces: "+(str1.length() - spaceless.length()));

Comments

0

You could simply do

String temp = str1.replaceAll("\\s+","");

temp.length() will give you the answer.

You can get rid of temp variable if modifying str1 is an option

Comments

0

Have you tried using the static method:

Character.isWhitespace(char ch);

For example,

if(!Character.isWhitespace(str1.charAt(i)))
    count++;

Comments

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.