aspose file tools*
The moose likes Beginning Java and the fly likes Parsing an Int Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Parsing an Int" Watch "Parsing an Int" New topic
Author

Parsing an Int

Kassi Hill
Greenhorn

Joined: Sep 29, 2003
Posts: 24
I was hoping someone could tell me if you could parse a long into an int and if you can, how you do it??
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Moving this to Java in General (Beginner) Forum...


GenRocket - Experts at Building Test Data
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
You mean something like this:
long lg = 5000;
int i = (int)lg;
All you need to do is cast the long into an int. If the long is too long for the int then you will get a wrong result.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Kassi Hill
Greenhorn

Joined: Sep 29, 2003
Posts: 24
Thank you for your help.
Tom Blough
Ranch Hand

Joined: Jul 31, 2003
Posts: 263
Kassi, Thomas is correct in that you only need to perform a cast to convert the number. He also points out the downside to the "narrowing conversion".
If you are concerned that the number is converted correctly, you can check that the long value is not greater than Integer.MAX_VALUE before attempting the cast, or you can verify that i == lg after the conversion.


Tom Blough<br /> <blockquote><font size="1" face="Verdana, Arial">quote:</font><hr>Cum catapultae proscriptae erunt tum soli proscripti catapultas habebunt.<hr></blockquote>
 
wood burning stoves
 
subject: Parsing an Int