File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Strings" Watch "Strings" New topic


Lesole Mphinyane
Ranch Hand

Joined: Oct 07, 2006
Posts: 42
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

Joined: Aug 25, 2005
Posts: 536
split the string into char array...loop through....count.....

MCSD, SCJP, SCWCD, SCBCD, SCJD (in progress - URLybird 1.2.1)
Deepak Bala

Joined: Feb 24, 2006
Posts: 6662

You could also use regex and the Pattern and Matcher classes

SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Lesole Mphinyane
Ranch Hand

Joined: Oct 07, 2006
Posts: 42
I stil can't get it.. pliz give me small code may be i can understand...
N Az

Joined: Jan 28, 2007
Posts: 6
May be you can try sth similar to
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Alan Moore
Ranch Hand

Joined: May 06, 2004
Posts: 262
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 ]
I agree. Here's the link:
subject: Strings
It's not a secret anymore!