This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes how to know the string is number or charactor? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to know the string is number or charactor?" Watch "how to know the string is number or charactor?" New topic
Author

how to know the string is number or charactor?

lijun wang
Greenhorn

Joined: Jan 20, 2006
Posts: 28
how to know the string is number or charactor?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Basically, a String is an object representing a sequence of characters. One way to access particular characters within a String is by calling String's charAt(int index) method. Once you have that char, you can test whether it's a digit or a letter by calling Character.isDigit(char c) or Character.isLetter(char c).


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Depending on your requirements a regular expression might be useful.



That's a simple example where b will be true if the entire String myString is composed of digits.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18492
    
  40

The String class actually provides a shortcut method for this...



Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
lijun wang
Greenhorn

Joined: Jan 20, 2006
Posts: 28
I tried to use this:
boolean b = myString.matches("\\d+");
but has the error in RAD:
The method matches(String) is undefined for the type String

WHY? I checked java API, string has this method!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18492
    
  40

Originally posted by lijun wang:

WHY? I checked java API, string has this method!


The regular expression engine was added to Java 1.4... What version of Java are you using?

Henry
lijun wang
Greenhorn

Joined: Jan 20, 2006
Posts: 28
I use RAD 6.0.1, is its java version 1.3, I can't believe, how to check the java version of RAD?
Srinivas Kalvala
Ranch Hand

Joined: Oct 20, 2005
Posts: 257

Originally posted by lijun wang:
I use RAD 6.0.1, is its java version 1.3, I can't believe, how to check the java version of RAD?


You can Check the version very easily.

Follow the following procedure.

-----------------------------

1) Select WINDOW --> Preferences in RAD.

2) in the Left panle expand JAVA

3) Select Installed JREs

4) Then It will show you the JRE it is using,,

5) if it is 1.3 then change it to point to the installed 1.4 or higger.

But

RAD 6.* comes with JRE 1.4 >.

Check your installation.

Thank you.

lijun wang
Greenhorn

Joined: Jan 20, 2006
Posts: 28
I install a jre5 and set it as RAD dfault jre, but I still can't import java.util.regex, and String has no matches method, why???
Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to know the string is number or charactor?
 
Similar Threads
a question about conversion
really need help about textfield & textarea
Which Monty Python character are you?
how to send & by appending the URL
how to validate a string like "20060503" is DATE