File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes I need to put a double into a vector Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "I need to put a double into a vector " Watch "I need to put a double into a vector " New topic

I need to put a double into a vector

Donna Bachner
Ranch Hand

Joined: Oct 08, 2004
Posts: 109
I have a vector and want to replace the number in the vector with a double how do I do it. This is what I have and keep getting a number format exception.

for (int m = 0; m<POInum; m++)
for (int n=0; n<len; n++)
Integer g = (Integer)POI[m].elementAt(n);
String s = g+"";

int f = Integer.parseInt(s);
double w = f * POIBottom[m];
String h = w+"";
Integer I = Integer.valueOf(h);// my error happens here

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24183

A double will have a decimal point in it, which isn't going to be a valid String representation of an integer. If you needed to convert a double into an int by way of a String, then you'd want to convert the double into an int first before converting to a String:

String s = ((int) w) + "";
int i = Integer.parseInt(s);

But you're doing a lot of slow, inefficient, and unnecessary conversions here. I really can't tell what you're trying to accomplish, exactly, but note that, for example, if you have an Integer object, you can use its "intValue()" method to make it into an int, without creating a String and parsing it:

Integer g = (Integer)POI[m].elementAt(n);
int f = g.intValue();

And you can make an Integer out of a double with help from a cast:

double w = f * POIBottom[m];
Integer I = new Integer((int) w);

The techniques I've shown here are much faster and more efficient, and can't give any runtime exceptions.

[Jess in Action][AskingGoodQuestions]
Donna Bachner
Ranch Hand

Joined: Oct 08, 2004
Posts: 109
I'll try that I know i was doing extra steps but did not know how to change it. Thanks
I agree. Here's the link:
subject: I need to put a double into a vector
Similar Threads
Object to int?
Wanted: Java2D example of zooming and panning with scroll bars
June Newsletter Puzzle
Problem with ProgressMonitor