Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > test if a string is an integer?

Reply
Thread Tools

test if a string is an integer?

 
 
dave
Guest
Posts: n/a
 
      07-16-2004
I am reading input from a form. I want to validate the input by making sure
that the string is actually an integer. How would I do this? Do i need to
convert it to a character array and break down each character and test it?
or is there an easier way? Thanks.


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.720 / Virus Database: 476 - Release Date: 7/14/04


 
Reply With Quote
 
 
 
 
Murray
Guest
Posts: n/a
 
      07-16-2004

"dave" <(E-Mail Removed)> wrote in message
news:FAHJc.59786$(E-Mail Removed)...
> I am reading input from a form. I want to validate the input by making

sure
> that the string is actually an integer. How would I do this? Do i need to
> convert it to a character array and break down each character and test it?
> or is there an easier way? Thanks.
>


Try to perform Integer.parseInt(yourString) and if it throws a
NumberFormatException you'll know the string isn't a valid integer


 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      07-16-2004
On Fri, 16 Jul 2004 03:04:05 GMT, "dave"
<(E-Mail Removed)> wrote or quoted :

>I am reading input from a form. I want to validate the input by making sure
>that the string is actually an integer.


there is no such group as comp.lang.java.

Just convert it and catch the exception if it is invalid.
See http://mindprod.com/jgloss/converter.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Murray
Guest
Posts: n/a
 
      07-16-2004
> there is no such group as comp.lang.java.

I've heard several people mention this, yet comp.lang.java does seem to
exist. Do you mean it's now defunct and should not be used?


 
Reply With Quote
 
Gary Labowitz
Guest
Posts: n/a
 
      07-16-2004
"dave" <(E-Mail Removed)> wrote in message
news:FAHJc.59786$(E-Mail Removed)...
> I am reading input from a form. I want to validate the input by making

sure
> that the string is actually an integer. How would I do this? Do i need to
> convert it to a character array and break down each character and test it?
> or is there an easier way? Thanks.


Hand the String to parseInt and catch NumberFormatException.
see:
http://java.sun.com/j2se/1.4.2/docs/...va.lang.String)
--
Gary


 
Reply With Quote
 
Michael N. Christoff
Guest
Posts: n/a
 
      07-16-2004

"Roedy Green" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> there is no such group as comp.lang.java.
>


There is, its just not a 'core' group and hence not carried by all usenet
servers.



l8r, Mike N. Christoff



 
Reply With Quote
 
Liz
Guest
Posts: n/a
 
      07-16-2004

"Murray" <(E-Mail Removed)> wrote in message
news:6PHJc.3087$(E-Mail Removed)...
>
> "dave" <(E-Mail Removed)> wrote in message
> news:FAHJc.59786$(E-Mail Removed)...
> > I am reading input from a form. I want to validate the input by making

> sure
> > that the string is actually an integer. How would I do this? Do i need

to
> > convert it to a character array and break down each character and test

it?
> > or is there an easier way? Thanks.
> >

>
> Try to perform Integer.parseInt(yourString) and if it throws a
> NumberFormatException you'll know the string isn't a valid integer
>


Do you really mean integer? Or is a "number" ok, then you can use
Double.parseDouble(string); which will return a result for integers,
floats, and doubles.


 
Reply With Quote
 
Chris Dutton
Guest
Posts: n/a
 
      07-16-2004
As an alternative to the solution already provided...

import java.util.regex.*;

....

Pattern integerPattern = Pattern.compile("^\d*$");
Matcher matchesInteger = integerPattern.matcher(myString);
boolean isInteger = matchesInteger.matches();

or:

boolean isInteger = Pattern.matches("^\d*$", myString);
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      07-16-2004
On Fri, 16 Jul 2004 03:24:57 GMT, "Murray"
<(E-Mail Removed)> wrote or quoted :

>I've heard several people mention this, yet comp.lang.java does seem to
>exist. Do you mean it's now defunct and should not be used?


see http://mindprod.com/jgloss/newsgroups.html


It existed a decade or so ago.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Tony Morris
Guest
Posts: n/a
 
      07-16-2004
> I am reading input from a form. I want to validate the input by making
sure
> that the string is actually an integer. How would I do this? Do i need to
> convert it to a character array and break down each character and test it?
> or is there an easier way? Thanks.


This question has been asked many times.
You'll receive answers ranging from checking that each char is between '0'
and '9' through to the use of regular expressions.
All of these suggestions are severely flawed, offering no benefit, and in
most cases creating a hindrance (i.e. not neutral side-effects).

The reason that these suggestions are attempted is because nobody (assuming
everybody knows what they are doing) likes catching a RuntimeException, and
especially not for the purpose of control flow. There is no suitable
alternative.

If it really bothers you (as it does me), encapsulate the "brokenness" in a
single method:

public boolean isParsableToInt(String i)
{
try
{
Integer.parseInt(i);
return true;
}
catch(NumberFormatException nfe)
{
return false;
}
}

Note that there is a slight performance penalty for doing this - the only
suitable alternative is to simply catch the RuntimeException each time an
attempt to parse is made.

--
Tony Morris
http://xdweb.net/~dibblego/


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
test test test richard Computer Support 3 01-24-2007 05:18 AM
TEST TEST Test...Blah Blah Blah generalbatguano@pacbell.net Computer Support 2 09-15-2006 03:47 AM
TEST TEST Test...Blah Blah Blah Generalbatguano@pacbell.net Computer Support 6 09-13-2006 01:53 AM
TEST TEST TEST Gazwad Computer Support 2 09-05-2003 07:32 PM
test test test test test test test Computer Support 2 07-02-2003 06:02 PM



Advertisments