This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
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 OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Strings" Watch "Strings" New topic
Author

Strings

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
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

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
Greenhorn

Joined: Jan 28, 2007
Posts: 6
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

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 ]
 
 
subject: Strings