Personal computing discussed

Moderators: renee, SecretSquirrel, just brew it!

 
malebolgia
Gerbil Elite
Topic Author
Posts: 973
Joined: Fri Apr 05, 2002 7:00 pm
Location: USA

Java Problem!

Wed Sep 10, 2003 3:44 pm

I'm trying to learn Java. One of the problems in the book is to write a program that will count the number of spaces that appear in the string, the number of letters, the number of digits, and it has to count all other special characters. My problem is I don't know where to start. So any help would be appreciated.
 
thegleek
Darth Gerbil
Posts: 7460
Joined: Tue Jun 10, 2003 11:06 am
Location: Detroit, MI
Contact:

Wed Sep 10, 2003 3:57 pm

––•–√\/––√\/––•–– nostalgia is an emotion for people with no future ––•–√\/––√\/––•–-
 
LJ
Gerbil Elite
Posts: 883
Joined: Wed Apr 10, 2002 2:24 am
Contact:

Wed Sep 10, 2003 4:32 pm

Here is a link to Sun's Java documentation. It's similar to other documentation (Microsoft's C++ documentation, for example) but better in most ways. In the specific case of MS the Sun documentation is a little more complicated but covers a lot more information and gives you a far better feel for how the language works as a whole and not just howt o "do" something.

http://java.sun.com/j2se/1.4.1/docs/api/index.html

I think you'll find that most tasks under java are surprisingly easy (well, very each compared to, say, a performance language such as C but fairly hard compared to a **** language like perl).

Specifically, you want to check out java.lang.String and java.util.regex. There are a ton of methods that will help you--the most nieve way would probably be to do something along these lines (I'll assume you know type types):

import java.lang.String;
import java.util.regex.*;

class test {
public static void main(String []args) {

   String stringName="123a bc 45 6defg l\t0lNH";
   int letters=0,digits=0;

   for (int n=0;n<stringName.length();n++) {
           if( Pattern.matches([a-zA-Z],stringName.charAt(n))  ) {
                   letters++;
           } else if ( Pattern.matches([0-9],stringName.charAt(n))  ) {
                   digits++;
           } // if-else-if
   } // for loop

   System.out.println("numbers: "+digits+"\nletters: "+letters);

} // main
} // class test

There are far quicker ways to accomplish this (for one, you'd want to be compiling the regular for reuse) and i'm pretty sure this won't compile (it's closer to perl than java) but it gives you the general idea. So i'm leaving a little excersize for you, i guess. I haven't actually done regular expressions in java without using Jlex so you'll probably run into a problem with those.

If i had to do it myself I'd try to make the code a little more concise and change the decision statements to something slightly more elegant.
 
Yahoolian
Grand Gerbil Poohbah
Posts: 3577
Joined: Sun Feb 16, 2003 3:43 pm
Location: MD
Contact:

Wed Sep 10, 2003 6:43 pm

http://java.sun.com/j2se/1.4.2/docs/api/

Hopefully, you know what a for loop is and how to make one
str = String you are trying to count the letters numbers digits spaces and specials...

// for loop until end of string
Character c = new Character( str.getCharAt( cnt ) );
if( c.isDigit() )
// you should know what to do here...
else if( c.isLetter() )
// and here...
else if( c.isSpaceChar() )
// and here....
else
// and here.

end for loop
That should be enough
 
malebolgia
Gerbil Elite
Topic Author
Posts: 973
Joined: Fri Apr 05, 2002 7:00 pm
Location: USA

Wed Sep 10, 2003 8:56 pm

Thanks for all the help guys I think I figured out how to go about with this program. :wink:

Who is online

Users browsing this forum: No registered users and 1 guest
GZIP: On