• 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

if test with or

 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Temporary insantiy has set in.

i was declared as
int i = 0;

command line variable parsed to integer
int i = Integer.parseInt ( args[0] );

i is tested for action
if ( i > 999999999999 || i < 0 )

error given on compile is:
integer number too large
(and how the hell do you copy out of command prompt?)

Integer should hold 999999999999 right?
Did I do the "or" || right?
What did I miss?
Thanks in advance.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nope, 12 9s is too big for int. Can you try long?

http://www.surfscranton.com/Architecture/JavaDataRanges.htm
 
Matt Fielder
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess I misread 2e31.
I figured it'd fit. So if I change that to long, I need to modify another section of my prog.

I think this required change is my problem now as I get the same error.

If I have:
int j = i / 1000000000
I don't think this'll automatically parse to int as Java knows its will lose accuracy. Do I have to have to cast this to int to force it?
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are sure the result of the division is small enough, you can do something like this:


So yes, you DO have to cast it.

The problem here is just as you said, 1000000000 is too big for an int, so the compiler gives it a type of long. In order to do the division, i is then promoted to long as well, so the result is also a long.

Layne
reply
    Bookmark Topic Watch Topic
  • New Topic