Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strings

 
Lesole Mphinyane
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys wassup! i need your help...
Given a string, how do you count number of white spaces in it, together with number of digits?... waiting to hear from you soon..
 
Richard Green
Ranch Hand
Posts: 536
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
split the string into char array...loop through....count.....
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could also use regex and the Pattern and Matcher classes
 
Lesole Mphinyane
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I stil can't get it.. pliz give me small code may be i can understand...
 
N Az
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May be you can try sth similar to http://java.sun.com/docs/books/tutorial/essential/regex/examples/SplitDemo.java
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If those answers sound too advanced, let's look at doing the work in smaller steps.

The String class has a method that will give you an array of char. The Character class has methods to tell if a char is a digit or whitespace.

Can you imagine a loop through the array, checking each character, adding to counters for digits and whitespace? Give it a shot and show us what you make. We can help you best with "almost working" code.
 
Alan Moore
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regexes are definitely not the tool for this job. It will be far easier to iterate through the characters yourself, as Stan suggested (and that's almost certainly what your instructor is expecting you to do).
[ January 31, 2007: Message edited by: Alan Moore ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic