This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Return a true integer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Return a true integer" Watch "Return a true integer" New topic
Author

Return a true integer

Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1543
    
    1
dResults2[3] is 4.000

I am tying to use to get 4 to use in a loop:

int x = Integer.parseInt(dResults2[3]);

What am I doing wrong?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

The parseInt method takes a String argument. What is the type of dResults2[3]? If it's a primitive floating point (float or double), then just use an explicit cast (which will truncate)...

int x = (int)dResults2[3];
[ December 27, 2007: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 43970
    
  33
Having seen Marc Weber's post:
You mean

x = (int)Double.parseDouble(dResult2[3]);

presumably?

Is there any risk that Double.parseDouble("4.000") could return 3.99999999999999999? I which case you will get an out-by-one error when it casts to 3. Just as you don't use floating-point arithmetic for money, you don't want to use it in "for" loops.
 
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: Return a true integer
 
It's not a secret anymore!