• Post Reply Bookmark Topic Watch Topic
  • New Topic

Storing numbers as variable type long  RSS feed

 
Tom Hong
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


That's all I have in my code right now and I'm getting an error on line 3 that reads "integer number too large: 600851475143." It was to my understanding that a long variable can store numbers way bigger than that. What am I doing wrong here?

Thanks in advance for any help.
 
Tom Reilly
Rancher
Posts: 618
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, a long can hold any number that can be put into an int. But by default, numbers in Java are int and 600851475143 will not fit into an int. Try adding an 'L' to the number, which tells the compiler it is a long:
 
Tom Hong
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wow that was simple. Thanks a lot for the help.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Tom.

Hey, that looks like Project Euler problem 3
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom Reilly wrote: . . . Try adding an 'L' to the number . . .
The compiler will happily accept lower-case l but always use upper-case, unless you want to confuse everybody who will think you meant 600851475141
 
Tom Hong
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Welcome to the Ranch, Tom.

Hey, that looks like Project Euler problem 3


That's exactly what it is.

Thanks for all the tips and help. Seems like a great community here.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!