The Artima Developer Community
Sponsored Link

Java Answers Forum
string to long

7 replies on 1 page. Most recent reply: Jul 1, 2003 5:55 AM by jennifer

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 7 replies on 1 page
jennifer

Posts: 6
Nickname: chil
Registered: Jun, 2003

string to long Posted: Jun 30, 2003 2:44 AM
Reply to this message Reply
Advertisement
hi
Im trying to convert a string to a long value

String s="test";

long long_test=Long.parseLong(s);
I get this error-
Exception occurred during event dispatching:
java.lang.NumberFormatException: test
at java.lang.Long.parseLong(Unknown Source)
at java.lang.Long.parseLong(Unknown Source)


Any ideas?


John Channing

Posts: 17
Nickname: drc
Registered: Jun, 2003

Re: string to long Posted: Jun 30, 2003 3:33 AM
Reply to this message Reply
For it to work, your string needs to look like a long number, for example, String test = "1234", should work just fine.
John

jennifer

Posts: 6
Nickname: chil
Registered: Jun, 2003

Re: string to long Posted: Jun 30, 2003 6:10 AM
Reply to this message Reply
Thanks for that

In my program,users are entering in abbreviated dates like dec1995.
I want to be able to convert this to a long.Is there no way,directly or indirectly I can do this?
thanks

Vance Arocho

Posts: 5
Nickname: aroc725
Registered: Jun, 2003

Re: string to long Posted: Jun 30, 2003 12:40 PM
Reply to this message Reply
Have you considered using regular expressions?

zenykx

Posts: 69
Nickname: zenykx
Registered: May, 2003

Re: string to long Posted: Jun 30, 2003 4:32 PM
Reply to this message Reply
You must define what the user can enter. Afterwards i think you got many solutions. If you cannot find 'em try to describe what the user is allowed to enter and maybe someone here will help you.

Channing Walton

Posts: 32
Nickname: channing
Registered: May, 2003

Re: string to long Posted: Jul 1, 2003 4:02 AM
Reply to this message Reply
Hi,
Can you not use java.text.SimpleDateFormat?

Channing

John Channing

Posts: 17
Nickname: drc
Registered: Jun, 2003

Re: string to long Posted: Jul 1, 2003 5:41 AM
Reply to this message Reply
>>In my program,users are entering in abbreviated dates like dec1995.
I want to be able to convert this to a long.Is there no way,directly or indirectly I can do this?<<

It depends what you mean. The quickest and easiest solution would be:

String date = "dec1996";
long l = date.hashCode();

Whether this is appropriate largely depends on what you want to do with the long after you have created it.

As you may or may not know, java.util.Date usese a floating point number to represent the date as a number of milli-seconds since January 1st 1970. A more sophisticated approach would therefore be to parse the String into a Date and then use the getTime() function to get at this number.
John

jennifer

Posts: 6
Nickname: chil
Registered: Jun, 2003

Re: string to long Posted: Jul 1, 2003 5:55 AM
Reply to this message Reply
Thanks Guys
Im currently looking at all your suggestions and trying to work out which one will involve the least work!!!
Will keep you posted.
thanks again

Flat View: This topic has 7 replies on 1 page
Topic: Pl. help java.security.AccessControlException: access denied Previous Topic   Next Topic Topic: Calling rollback on a Connection released to the pool

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use