• Post Reply Bookmark Topic Watch Topic
  • New Topic

if test with or  RSS feed

 
Matt Fielder
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • 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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!