Counting all Non-blank Characters in a String

Thread Solved

Join Date: Sep 2007
Posts: 2
Reputation: Kevin008 is an unknown quantity at this point 
Solved Threads: 0
Kevin008 Kevin008 is offline Offline
Newbie Poster

Counting all Non-blank Characters in a String

 
0
  #1
Sep 13th, 2007
I am just starting out at JAVA and trying to write a program that counts all characters in a string, counts all characters minus trailing and leading blanks and also counts all non-blank characters. Here is what I have so far. I am stuck on the non-blank character counts. Can anyone provide some help on this? The countChar always returns 0...

  1. import java.util.Scanner;
  2.  
  3. public class Count {
  4.  
  5. public static void main(String[] args) {
  6.  
  7. for (int count = 1; count <= 5; count ++) {
  8.  
  9. System.out.println("Please enter some text: ");
  10. Scanner keyboard = new Scanner(System.in);
  11. String text = keyboard.nextLine();
  12.  
  13. int HowLong = text.length();
  14. String MinusTrailing = text.trim();
  15. int CMinusTrailing = MinusTrailing.length();
  16.  
  17. String NB = MinusTrailing;
  18. int countChar = 0;
  19. int limit = NB.length();
  20.  
  21. for(int i = 0; i < limit; ++i)
  22. {
  23. if(Character.isWhitespace(NB.charAt(i)))
  24. {
  25. ++count;
  26. }
  27. }
  28.  
  29. System.out.println(HowLong);
  30. System.out.println(CMinusTrailing);
  31. System.out.println(countChar );
  32.  
  33.  
  34. }
  35.  
  36. }
  37.  
  38. }

Thanks!
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,445
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Counting all Non-blank Characters in a String

 
0
  #2
Sep 13th, 2007
I think you want to increment "countChar "instead of "count" in this statement
  1. if(Character.isWhitespace(NB.charAt(i)))
  2. {
  3. ++count;
  4. }
Last edited by Ezzaral; Sep 13th, 2007 at 4:11 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 2
Reputation: Kevin008 is an unknown quantity at this point 
Solved Threads: 0
Kevin008 Kevin008 is offline Offline
Newbie Poster

Re: Counting all Non-blank Characters in a String

 
0
  #3
Sep 13th, 2007
Originally Posted by Ezzaral View Post
I think you want to increment "countChar "instead of "count" in this statement
Got it!! Thank you so much!!!
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,386
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 252
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Counting all Non-blank Characters in a String

 
0
  #4
Sep 14th, 2007
  1. int countChar = string.replaceAll("\\s*", "").length();

Just to be an @ss! ;-)
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC