Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

toLong? 2

Status
Not open for further replies.

stormbind

Technical User
Mar 6, 2003
1,165
GB
Hi, is there a method which converts a string to a long integer?

--Glen :)

Memoria mihi benigna erit qui eam perscribam
 
String s = "88888888888888888888888888888888888888888";
long l = Long.parseLong(s);

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
It looked good until..

Exception in thread "main" java.lang.NumberFormatException: For input string: "
4905"

There's a character space before the number. Does java have a quick fix, or do I have to use RegExp?

--Glen :)

Memoria mihi benigna erit qui eam perscribam
 
What about .trim() method of String class?

That will remove leading and trailing spaces.

Cheers,
Dian
 
Awsome, thanks! :D

--Glen :)

Memoria mihi benigna erit qui eam perscribam
 
If the String could be null, make sure you check for null before performing trim() !

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
sedj is right on...good practice to surround your trim() and parseXXX() methods with try/catch to handle both null AND invalid entries (in your case, non-numeric). Then you can exchange that ugly java generated exception with a more user-friendly message and/or corrective logic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top