Be a Supporter!

String Checking Program (java)

  • 414 Views
  • 10 Replies
New Topic Respond to this Topic
ClassicOverflow
ClassicOverflow
  • Member since: Apr. 29, 2012
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
String Checking Program (java) May. 30th, 2012 @ 07:27 PM Reply

Found this little snippet of code while perusing another coding forum. It takes a string and checks to see if it is a number or not. Hope it helps any new java programmers out there.

String Checking Program
if ("-2324.00".matches("((-|\\+)?[0-9]+(\\.[0-9]+)?)+")) {
System.out.println("Is a number");
} else {
System.out.println("Is not a number");
}
}
}

SeeD419
SeeD419
  • Member since: Mar. 24, 2003
  • Offline.
Forum Stats
Member
Level 27
Programmer
Response to String Checking Program (java) May. 30th, 2012 @ 08:10 PM Reply

At 5/30/12 07:27 PM, ClassicOverflow wrote: Found this little snippet of code while perusing another coding forum. It takes a string and checks to see if it is a number or not. Hope it helps any new java programmers out there.

String Checking Program
if ("-2324.00".matches("((-|\\+)?[0-9]+(\\.[0-9]+)?)+")) {
System.out.println("Is a number");
} else {
System.out.println("Is not a number");
}
}
}

I'm not really a fan of using regex to validate numbers personally. I usually just do:

try {
    Integer.parseInt(teststring);
} catch (NumberFormatException e) {
    //If you reach here it's not a number.
}

Of course, you can use whatever class of input you're expecting. Double.parseDouble, etc.


Minecraft server: proxa.co
PortalWatch 0.1 Alpha - Download
Sig by illicit

BBS Signature
Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to String Checking Program (java) Jun. 1st, 2012 @ 11:17 AM Reply

That regex is overly verbose and has no support for numbers with scientific notation.
Instead of copying/pasting poorly written code from arbitrary sources on the Internet you should learn what they're doing.

ClassicOverflow
ClassicOverflow
  • Member since: Apr. 29, 2012
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to String Checking Program (java) Jun. 1st, 2012 @ 10:17 PM Reply

At 5/30/12 08:10 PM, SeeD419 wrote: I'm not really a fan of using regex to validate numbers personally. I usually just do:

try {
Integer.parseInt(teststring);
} catch (NumberFormatException e) {
//If you reach here it's not a number.
}

Of course, you can use whatever class of input you're expecting. Double.parseDouble, etc.

I wasn't really familiar with parseInt or try until now. Thanks for the advice!

EuPlonKa
EuPlonKa
  • Member since: Dec. 18, 2007
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to String Checking Program (java) Jun. 14th, 2012 @ 09:01 PM Reply

At 6/1/12 10:17 PM, ClassicOverflow wrote:
At 5/30/12 08:10 PM, SeeD419 wrote: I'm not really a fan of using regex to validate numbers personally. I usually just do:

try {
Integer.parseInt(teststring);
} catch (NumberFormatException e) {
//If you reach here it's not a number.
}

Of course, you can use whatever class of input you're expecting. Double.parseDouble, etc.
I wasn't really familiar with parseInt or try until now. Thanks for the advice!

My understanding is that a try/catch is pretty expensive, and should be avoided if possible. If a non-scientific notation number checker is all you need then this would be fine.

Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to String Checking Program (java) Jun. 15th, 2012 @ 08:23 AM Reply

At 6/14/12 09:01 PM, EuPlonKa wrote: My understanding is that a try/catch is pretty expensive, and should be avoided if possible. If a non-scientific notation number checker is all you need then this would be fine.

I'm not very experienced with Java but I'm willing to bet that a regular expression is more expensive than a try/catch block.
I also found another flaw in that regex: it has no support for commas; any numbers with the 1000 decimal places separated by commas will not be parsed properly. It also doesn't seem to support regular decimal places either.

These are the results of me toying with the OP's regex:

1,657,414.155 // Treated as four numbers: "1", "657", "414", and "155".
5\522-5\1 // Treated as a single number. Backslashes and hyphen don't affect it.
+4200 // Treated as "4200"; plus sign ignored.
-4200 // Treated as "-4200"; negative sign recognised.

This is pretty much a shining example of why you shouldn't use regular expressions when you don't understand them.
If the OP wants to learn this is an excellent, albeit incomplete, resource.

Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to String Checking Program (java) Jun. 15th, 2012 @ 08:32 AM Reply

Nevermind, the OP's regex does indeed support decimal places; the double backslashes was throwing off my regex program (which is why it allowed me to have backslashes in the middle of a number).
Still, it treats "1-2+3-4+5" as a valid number, which is obviously a glitch.

egg82
egg82
  • Member since: Jun. 24, 2006
  • Online!
Forum Stats
Member
Level 05
Game Developer
Response to String Checking Program (java) Jun. 15th, 2012 @ 11:05 AM Reply

At 6/15/12 08:32 AM, Diki wrote: Still, it treats "1-2+3-4+5" as a valid number, which is obviously a glitch.

that's an interesting glitch. So if you put that into a number, would it come out as 15 or a null?
If it comes out as 15, you wouldn't have to worry about it too much.


Programming stuffs (tutorials and extras)
PM me (instead of MintPaw) if you're confuzzled.
thank Skaren for the sig :P

BBS Signature
Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to String Checking Program (java) Jun. 15th, 2012 @ 11:15 AM Reply

It won't treat it as a mathematical operation; just a string.
The glitch happens because the entire expression is wrapped in a block, which uses the + operator. So it's searching for a number, which can begin with a positive/negative sign, and then will search within numbers for more numbers. So it sees "1+2+3+4" as the numbers "+1", "+2", "+3", and "+4" (which by themselves are all valid numbers), and then combines them into a single match.

For comparison, I rewrote the OP's regex so that it matches numbers properly:

[-+]?[0-9]+([.][0-9]+)?

Still won't match scientific notation though (not that that would be difficult).

Hoeloe
Hoeloe
  • Member since: Apr. 29, 2004
  • Offline.
Forum Stats
Member
Level 37
Game Developer
Response to String Checking Program (java) Jun. 17th, 2012 @ 05:13 AM Reply

At 6/15/12 08:23 AM, Diki wrote:
I'm not very experienced with Java but I'm willing to bet that a regular expression is more expensive than a try/catch block.

That very much depends on the amount of data you're trying to analyse. Regex string matching has a high initial overhead, but is very fast once that's been set up. So for small amounts of data, you're just seeing the initial overhead, but with larger data, the speed difference will be greater.

Still, in this particular case, I don't think it makes much of a difference.


Decima: The Last Story of Vald has a Facebook page and a development blog. Give them a look!
------------------------------

BBS Signature
EuPlonKa
EuPlonKa
  • Member since: Dec. 18, 2007
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to String Checking Program (java) Jun. 17th, 2012 @ 07:21 PM Reply

At 6/17/12 05:13 AM, Hoeloe wrote:

:Regex string matching has a high initial overhead, but is very fast once that's been set up.

By setup are you referring to the call to Pattern.compile()? Because for something like this, you can create a static/singleton for the compiled pattern which would alleviate this right?