Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

isString, isNumeric or something

  Asked By: Pedro    Date: Jan 08    Category: Java    Views: 2794
  

I need to validate a data.
Im read a string and I have to know if a char reading is a string or
if is a number.

But I want to use same method.
Another solution is to have a if anidade, but Im think do a easy way
to do this or not?

if charReading="0" or charReading="1" or ......

Share: 

 

8 Answers Found

 
Answer #1    Answered By: Nicholas Wells     Answered On: Jan 08

I might have read  this wrong ... But you just want to check if a string
is a number  or not?

Check Integer.parseInt(stringValue); and Double.parseDouble(stringValue);

It can lead to some messy code, you can make neater code by using regX
... but you can worry about regX later

 
Answer #2    Answered By: Lily Brown     Answered On: Jan 08

There are many ways to do this task. exactly how depends on the type of
input stream you are reading. All of the classes that shadow Primitive
types (Integer, Boolean, Double, Long, etc) have a Foo.parse*(someString)
method which will converted someString to the type indicated by "*" where
Foo is the class name. Thus Integer.parseInt(someString) will return an int
value if the string  can be converted. if not then it will throw a
NumberFormatException which you can catch. Nothing stops you from doing
character by character compares and there are methods of the char  class
which can indicate with a boolean if a given character is a number  or a
character. But I would recommend looking at the stringTokenizer class, and
the buffered read  classes(for the type of stream you need to read). if you
are looking to validate  data inputs, I would recommend you to leave the
character by character checking to the already built java classes.

 
Answer #3    Answered By: Umaiza Hashmi     Answered On: Jan 08

Actually, I am familiar with the Integer, Double, etc.. objects.
But I am intrested in the regX you mentioned. Would you be kind enough to giv me
some more extra info, at least a link I can begin with.

 
Answer #4    Answered By: Barachias Levi     Answered On: Jan 08

You can do pretty much anything with regular expressions.

java.sun.com/.../package-frame.html

And a bunch of other tutoralis, that are off course scattered aroudn the
net.

 
Answer #5    Answered By: Naomi Lee     Answered On: Jan 08

I think I should look around more inside the jre and
see what packages included there, and at least, know what each one does.
After all, it's the standard edition

 
Answer #6    Answered By: Bathilda Schmidt     Answered On: Jan 08

It can be realy kind of hard to grasp how regX works. Once you start
down the dark path string  manipulation becomes amazingly simple.

 
Answer #7    Answered By: Joyce Edwards     Answered On: Jan 08

regex (regular expression, same with regX that u mean ?) can
lead messy code as well. It can slow down the performance if you are
not carefully (especially with greedy regex). You can learn about
regex by reading  Oreilly Book, Mastering Regular Expression (2nd
edition, covering Java Regex as well) or you can read  from perl regex
(pcre), search it in the internet. Java regex is compatible with perl
regex (may be it is compatible with posix regex as well, but imho,
perl regex is more complex and powerfull)

 
Answer #8    Answered By: Adel Fischer     Answered On: Jan 08

I would be realy carefull about using Regular Expressions. I use them
quite often because I am doing exact stuff, and I am working to a
standard. It can also lead to some messy code, or at least some code
that often needs a lot of refactoring. Not to mention that many people
have no idea what it is or how to modify it.

 
Didn't find what you were looking for? Find more on isString, isNumeric or something Or get search suggestion and latest updates.




Tagged: