| Author |
Cannot convert double to long..
|
Ann Ron
Greenhorn
Joined: Oct 21, 2003
Posts: 24
|
|
Hi, I am writing a code : double repid; double repstr; repid = Math.random( ); repstr = (long) ( repid * 10000000000000 ); I get this error : Integer Literal Out of Range,Decimal int literals must be in a certain range... I tried to make my repstr type "long" in order to accomodate the multiplication I do, but it still gives me the same error. error: Integer Literal Out of Range Any clue how to solve this.I know Java needs explicit casting also but I dont know what to do. Any help will be appreciated,Thanks in adavnce. Ann
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24051
|
|
Hi Ann, Welcome to JavaRanch! It's not complaining about the type of the variable, but the type of the "literal" -- i.e., the hard-coded value. The number you're writing is too large to represent as a 32-bit int, hence the error. You can write a long literal by putting an "L" after it, like 1000000000000L. But since you're assigning to a double, why not make it a double literal by putting a decimal point or a letter "D" at the end: "100000000000000." or "100000000000D" .
|
[Jess in Action][AskingGoodQuestions]
|
 |
Ann Ron
Greenhorn
Joined: Oct 21, 2003
Posts: 24
|
|
Thanks a lot,It worked.I feel so silly asking such a basic question but I am new to Java is my only cop-out. Thanks Ann
|
 |
Ann Ron
Greenhorn
Joined: Oct 21, 2003
Posts: 24
|
|
|
Hi Ernest,
|
 |
Ann Ron
Greenhorn
Joined: Oct 21, 2003
Posts: 24
|
|
Hi Ernest, The code compiles fine but it doesn't do what it is supposed to be doing.I need a 13 digit long number from repstr which is why i multiply it with 10000000000000.When I do like you suggested it still gives me only 7 digit long value.So I thought I would make repstr long and then do as follows : double repid; long repstr; repid = Math.random( ); repstr = repid * 10000000000000D ; But this gives me another error : Incompatible type for =. Explicit cast needed to convert double to long.How can I have the repstr to be 13 digits long and do this.PLease help. Thanks in advance. Ann
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24051
|
|
Can you show me what you're doing with repstr that's leading to the 7 vs. 13-digit number? I'm not quite sure what you're saying. [ October 21, 2003: Message edited by: Ernest Friedman-Hill ]
|
 |
Lisa D'Aniello
Ranch Hand
Joined: Sep 25, 2003
Posts: 42
|
|
Hi Ann, I'm not sure what your solution is for getting your 13 digit long, but in your code below you're doing the following: long = double * double (this is a no no) That should be the source of your current error. Hope this helps at least a little bit, Lisa M.
double repid; long repstr; repid = Math.random( ); repstr = repid * 10000000000000D ; But this gives me another error : Incompatible type for =. Explicit cast needed to convert double to long.How can I have the repstr to be 13 digits long and do this.PLease help. Thanks in advance. Ann
|
 |
Ann Ron
Greenhorn
Joined: Oct 21, 2003
Posts: 24
|
|
Hi, This is what I do with repstr.. double repid; long repstr; repid = Math.random( ); repstr = (long) repid * 10000000000000L ;//This is different repstr1 = String.valueOf( repstr ) ; repstr2 = repstr1.substring(13); doc.replaceItemValue("RepositoryID",repstr2 ); This repstr2 is actually giving me a 7 digit value instead of the 13 long. When I do the above,cast the repstr as long then it compiles fine but when I run this code,it gives me an error StringIndex out of bounds exception. I am trying all sorts of combinations of long and double and casting,nothin seems to be working..Any suggestions...? Thanks, Ann
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24051
|
|
OK, let's see. Each code example you've shown me is a little different and thus has slightly different problems. In this one, you may be surprised to learn that the line computes 0! The cast applies to "repid" alone, which gives 0 (casting to integer type truncates towards 0). If you add some parentheses around the whole expression, like this: then repstr will be a number which most of the time has thirteen digits. Sometimes it will have fewer though (if repid is between 0 and 0.1). OK, now, I can't even imagine what you're doing calling repstr1.substring(13). This function gives you the substring of a string starting with the character at the given index. For a 13-character string, this will be "", the empty string. For a shorter string, which this one will occasionally be, you'll get an index out of bounds problem. So basically you just want a 13-digit random string, is that it -- i.e., a number between 10E12 and 10E13. The classic forumla for a random number between A and B looks like So your string would be where I've added a few constants to make it easier to read. [ October 22, 2003: Message edited by: Ernest Friedman-Hill ]
|
 |
Ann Ron
Greenhorn
Joined: Oct 21, 2003
Posts: 24
|
|
That solved it.I now get a 13 digit long repstr. Thanks a tonne,for all your help and patience. Ann
|
 |
 |
|
|
subject: Cannot convert double to long..
|
|
|