• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Arithmetic Overflow in exercise 4b

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys...I need some help with exercise 4b.
Okay people, without giving too much away I'm trying to determine the number of
billions in a given number e.g. 555,444,333,222 .
To achieve this I need to divide it by 1000,000,000,000.
However, my compiler gives me the following error " Literal overflows capacity of int type ...":
The only way of performing such a calculation is by appending L to the value to indicate
that it is of type long.
i.e 555444333222L / 1000000000000L .

However since the number will actually be a variable, I somehow need to pass it into methods as a an argument without the expression above falling flat on its face.

I've almost completed 4b, and that's the remaining obstacle as I see it.

thanks in advance ...
 
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I am correct, the problem is with the math you are trying to accomplish. Look again at how much you are dividing by and what you are trying to seperate out.
Jason
 
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might consider handling it in string chunks
Dan
 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Eviano Afiemo:

However, my compiler gives me the following error " Literal overflows capacity of int type ...":
The only way of performing such a calculation is by appending L to the value to indicate
that it is of type long.
i.e 555444333222L / 1000000000000L .

However since the number will actually be a variable, I somehow need to pass it into methods as a an argument without the expression above falling flat on its face.


Why not make the variable a long instead of an int.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Eviano Afiemo:
... type long.
i.e 555444333222L / 1000000000000L .

However since the number will actually be a variable, I somehow need to pass it into methods as a an argument without the expression above falling flat on its face.


long number = 555444333222L / 1000000000000L
??

 
Eviano Afiemo
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your replies ....


  • Jason I see what you're saying about the math in the example I gave, I mistakenly added an extra zero, but that's not the cause of the problem (I wish it were though :-)).
  • [list]Richard, even when I declared the variables as type long I still got the same error.
    i.e when I did this:

    the error goes something like this :
    integer number too large: 123456789012
    long L1 = 123456789012 ;

    ^
    [/list]

  • long number = 555444333222L / 1000000000000L
    ??


    Marilyn I read in a book, that Java defaults to integers for number literals, and the only way to let the compiler know that you're dealing with a long is by appending an "L" to the end of the number. That's why the above code works.

  • I just wanted to know if anyone else had come across this problem. I find it so frustrating. I'll probably have to go with Dan's suggetion (breaking the number in to chunks). But still, I'd like to know how one would deal with the problem.

    that's all for now. Thanks people

    [This message has been edited by Eviano Afiemo (edited July 24, 2001).]
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Eviano Afiemo:
Marilyn I read in a book, that Java defaults to integers for number literals, and the only way to let the compiler know that you're dealing with a long is by appending an "L" to the end of the number. That's why the above code works.
[/list]


Yes, that is correct. Java defaults to integers for number literals. Try this one:

I'll probably have to go with Dan's suggetion (breaking the number in to chunks).


This is a better approach for this assignment.
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, do you know what the possible range of values is for an int?
 
Eviano Afiemo
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Marilyn deQueiroz:
By the way, do you know what the possible range of values is for an int?


Yes Marilyn, it's [ -2,147,483,648 - 2,147,483,647 ] ( at least that what it says in "Java Software Solutions by John Lewis and William Loftus" ) .
Thanks for your help, I've started over with my program, using the other approach.
 
Eviano Afiemo
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Although I've started over, I've also managed to solve the problem on yhe basis of my old approach. I'm now going to submit it like this, to see if I can learn anything from the nitpicks which I'm very sure I'll recieve.
Once again , thanks everybody.
One thing I can say is that it feels good when you solve a problem that's been bugging you for a while. By coincidence one of my JSP projects also fell into place today... It might have something to do with the good weather we've been having recently in England.
 
jason adam
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It doesn't feel good, it feels grrrrrrreat!
This one took me something like 13 tries to get right, so don't feel bad if your solution gets shot down several times, I think everyone learned a great deal from this assignment.
reply
    Bookmark Topic Watch Topic
  • New Topic