| | |
Counting all Non-blank Characters in a String
Thread Solved |
•
•
Join Date: Sep 2007
Posts: 2
Reputation:
Solved Threads: 0
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...
Thanks!
Java Syntax (Toggle Plain Text)
import java.util.Scanner; public class Count { public static void main(String[] args) { for (int count = 1; count <= 5; count ++) { System.out.println("Please enter some text: "); Scanner keyboard = new Scanner(System.in); String text = keyboard.nextLine(); int HowLong = text.length(); String MinusTrailing = text.trim(); int CMinusTrailing = MinusTrailing.length(); String NB = MinusTrailing; int countChar = 0; int limit = NB.length(); for(int i = 0; i < limit; ++i) { if(Character.isWhitespace(NB.charAt(i))) { ++count; } } System.out.println(HowLong); System.out.println(CMinusTrailing); System.out.println(countChar ); } } }
Thanks!
I think you want to increment "countChar "instead of "count" in this statement
Java Syntax (Toggle Plain Text)
if(Character.isWhitespace(NB.charAt(i))) { ++count; }
Last edited by Ezzaral; Sep 13th, 2007 at 4:11 pm.
Java Syntax (Toggle Plain Text)
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
----------------------------------------------
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
![]() |
Similar Threads
- Counting Alphabet Characters, Words, and String Length! (C)
- Counting specific characters in a string (C)
- Removing characters from a string (C)
- Counting Spaces in a string (C++)
- Counting uppercase characters (Java)
Other Threads in the Java Forum
- Previous Thread: syntax error in FROM clause
- Next Thread: java conainers
| Thread Tools | Search this Thread |
actuate android api applet application applications array arrays automation balls bank binary bluetooth business c++ chat class clear client code codesnippet collections component coordinates database defaultmethod development dice doctype dragging ebook eclipse educational error formatingtextintooltipjava fractal froglogic game givemetehcodez graphics gui hql html ide image infinite ingres input integer intersect invokingapacheantprogrammatically j2me java javaprojects jni jpanel jtextarea julia linux list loop looping map method methods mobile mysql netbeans newbie openjavafx parameter php print problem program programming project recursion repositories scanner scrollbar server set size sms sort sorting sql sqlserver state storm string sun superclass swing swt text-file threads tree windows






