String not being a valid number
Hi,
I'm processing a set of strings in a loop, and I just want to continue if certain Strings are not valid numbers. I don't want an Exception thrown, I just want to skip over that String that is not a valid number. Any suggestions.Thank you.
Parse the string to a number, and catch the NumberFormatException that might get thrown. It's not the end of the world, you can deal with the exception and move on, that's what exceptions are for
try {
Integer.parseInt(mayBeANumberString);
}
catch(NumberFormatException nfe) {
continue;
}
You could also use a method to check like so
public class DigitTest {
public static void main(String[] args) {
if(isNumeric("123x")) {
System.out.println("Yes");
} else {
System.out.println("No");
}
}
static boolean isNumeric(String input) {
for (int i = 0; i < input.length(); i++) {
if(!Character.isDigit(input.charAt(i))) {
return false;
}
}
return true;
}
}
The trouble with code like the last reply, is that what is often wanted
(although it has not been articulated yet) is that the string should
represent a number in a certain range, like a number that falls in
the range represented by int, versus allowing the string:
"9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999"
I suggest using the parseInt method (for example) with a catch block.
> You could also use a method to check like so
>
> > public class DigitTest {
>
> public static void main(String[] args) {
>
> if(isNumeric("123x")) {
> System.out.println("Yes");
> } else {
> System.out.println("No");
> }
> }
>
>
> static boolean isNumeric(String input) {
> for (int i = 0; i < input.length(); i++) {
> if(!Character.isDigit(input.charAt(i))) {
> return false;
> }
> }
> return true;
> }
> }
>
Only works for positive whole numbers, remember. Once a decimal point is or a sign is introduced, it gets tricky
Ok, fair point. I agrees with the first codez.
> Only works for positive whole numbers, remember. Once
> a decimal point is or a sign is introduced, it gets
> tricky
Mwoah...
static boolean isNumeric(String s) {
return s.matches("[-+]?((\\d*((?<=\\d)\\.|\\.(?=\\d))\\d*)|(\\d+))((?<=\\d)[eE][-+]?\\d+)?[dDfF]?");
}
; )
> > Only works for positive whole numbers, remember.
> Once
> > a decimal point is or a sign is introduced, it
> gets
> > tricky
>
> Mwoah...
>
> static boolean isNumeric(String s) {
> return
> s.matches("[-+]?((\\d*((?<=\\d)\\.|\\.(?=\\d))\\d*)|(
> \d+))((?<=\\d)[eE][-+]?\\d+)?[dDfF]?");
> }
>
> ; )
My boys got Mad Skills
> > Only works for positive whole numbers, remember.
> Once
> > a decimal point is or a sign is introduced, it
> gets
> > tricky
>
> Mwoah...
>
> static boolean isNumeric(String s) {
> return
> s.matches("[-+]?((\\d*((?<=\\d)\\.|\\.(?=\\d))\\d*)|(
> \d+))((?<=\\d)[eE][-+]?\\d+)?[dDfF]?");
> }
>
> ; )
Yeahhh, I agree with the last post now, scrap my other reply.
> > > Only works for positive whole numbers, remember.
> > Once
> > > a decimal point is or a sign is introduced, it
> > gets
> > > tricky
> >
> > Mwoah...
> >
> > static boolean isNumeric(String s) {
> > return
> >
> s.matches("[-+]?((\\d*((?<=\\d)\\.|\\.(?=\\d))\\d*)|(
> > \d+))((?<=\\d)[eE][-+]?\\d+)?[dDfF]?");
> > }
> >
> > ; )
>
> Yeahhh, I agree with the last post now, scrap my
> other reply.
Heh heh you know a bandwagon when you see one, eh! I'd still plump for mine on the basis that I don't really know how to construct a regex even that complex without a reference manual, and nor do other people. Making things easy for yourself and others to read code is very important IMHO
> ...
> Yeahhh, I agree with the last post now, scrap my
> other reply.
Well, it was just a little regex-test for fun* and I wouldn't suggest the OP to use it in his real-life code.
Reply #1 is (IMHO) the safest way to go about this: just (try) to parse it to whatever primitive you want, and catch a possible exception.
* I made it a couple of days back: so I didn't come up with it within the few minutes between george's and my replies.
> * I made it a couple of days back: so I didn't come
> up with it within the few minutes between george's
> and my replies.
And humble too. Still voodoo, if you ask me. I'm going to learn these things, out of sheer blo0dymindedness. Then you'll all be sorry
> > ...
> > Yeahhh, I agree with the last post now, scrap my
> > other reply.
>
> Well, it was just a little regex-test for fun* and I
> wouldn't suggest the OP to use it in his
> real-life code.
> Reply #1 is (IMHO) the safest way to go about this:
> just (try) to parse it to whatever primitive you
> want, and catch a possible exception.
>
> * I made it a couple of days back: so I didn't come
> up with it within the few minutes between george's
> and my replies.
Yeah I still think the first reply was best, much easier to write atleast.
Besides that thing was a bit scary. It reminded me of some AWK scripts I saw on a Unix forum and I just thought "what the hell is that".
> > > ...
> > > Yeahhh, I agree with the last post now, scrap my
> > > other reply.
> >
> > Well, it was just a little regex-test for fun* and
> I
> > wouldn't suggest the OP to use it in his
> > real-life code.
> > Reply #1 is (IMHO) the safest way to go about
> this:
> > just (try) to parse it to whatever primitive you
> > want, and catch a possible exception.
> >
> > * I made it a couple of days back: so I didn't
> come
> > up with it within the few minutes between george's
> > and my replies.
>
> Yeah I still think the first reply was best, much
> easier to write atleast.
>
> Besides that thing was a bit scary. It reminded me of
> some AWK scripts I saw on a Unix forum and I just
> thought wtf?
Guy I work with is always bragging about his flatmates (I know!) mad SED skills. Impressive though I'm sure they are, I don't go drinking with these boys
> Guy I work with is always bragging about his
> flatmates (I know!) mad SED skills. Impressive though
> I'm sure they are, I don't go drinking with these boys
I am a bit of a black belt in SED myself, for instance here is one of my special moves
sed 's/foo/bar/g'
I can delete to the end of the line with VI also.
Actually I use Ultra Edit's FTP as much as possible when I have to search log files at work.
> > Guy I work with is always bragging about his
> > flatmates (I know!) mad SED skills. Impressive
> though
> > I'm sure they are, I don't go drinking with these
> boys
>
> I am a bit of a black belt in SED myself, for
> instance here is one of my special moves
>
> sed 's/foo/bar/g'
>
> I can delete to the end of the line with VI also.
>
> Actually I use Ultra Edit's FTP as much as possible
> when I have to search log files at work.
I can tape all of your buns together :)
> I can tape all of your buns together :)
What's buns? oO
> Still voodoo, if you ask me.
Let's strip the voodoo from it then:
[-+]?an optional '-' or '+' sign
(
(
\\d* followed by zero or more numbers
(
(?<=\\d)\\.followed by a '.' with either a number in front of it
| OR
\\.(?=\\d)a number after it
)
\\d* followed by zero or more numbers
)
|OR
(
\\d+ one or more numbers
)
)
(
(?<=\\d)[eE]an 'e' or 'E' only if it has a number before it
[-+]? followed by an optional '-' or '+' sign
\\d+followed by one or more numbers
)?which is all optional (the previous three lines)
[dDfF]? ending with an optional 'd','D','f' or 'F'.
> I'm going to learn these things, out of sheer
> blo0dymindedness. Then you'll all be sorry
If you do, I recommend Mastering Regular Expressions by Friedl. I bought it because uncle_alice and sabre150 are of the opinion that this is "the book". And I must say, until so far I really like it: Friedl really "makes you think" in regex-es.
> > I can tape all of your buns together :)
>
> What's buns? oO
Watch "The Breakfast Club" for more details :-)