This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Problem with the converting Object to Double in the output 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 "Problem with the converting Object to Double in the output" Watch "Problem with the converting Object to Double in the output" New topic
Author

Problem with the converting Object to Double in the output

Derek Varghese
Greenhorn

Joined: Jun 21, 2011
Posts: 3
Hi,
I have the following arrays findVar{int} and radius{double}.They are assigned values from a Stack<Object>

Stack<Object> Savestack
private static int FIND_PARMS = 5;
private int[] findVar = new int[FIND_PARMS];
private double[] radius = new double[FIND_PARMS];

/******Code***************

findVar = (int[]) Savestack.get(i-24);
for (i = 0; i < FIND_PARMS; i++)
{
outputFile.print(findVar[i] + " ");
}

radius = (double[])Savestack.get(i-23);
for (i = 0; i < FIND_PARMS; i++)
{
outputFile.println(radius[i]);

}
I am able to write the int values out using findVar
but not radius.
Could you please tell me what is it that I am not doing right?

I get this error
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException:
-18
at java.util.Vector.get(Unknown Source)
at NewStatics.Parameter.saveParameters(Parameter.java:784
)
at NewStatics.Statics$SaveListener.actionPerformed(Static
s.java:630)
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11170
    
  16

if you are going to iterate through an array, you are usually better off to use the length:



There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

Derek Varghese wrote:findVar = (int[]) Savestack.get(i-24);
for (i = 0; i < FIND_PARMS; i++)
{
outputFile.print(findVar[i] + " ");
}

You use i for both the main stack access and for the loop variable. While initially i is probably high enough, it ends up being FIND_PARMS (5). You then use that same i again:
radius = (double[])Savestack.get(i-23);

With i being 5, i-23 is -18 and that's not a valid index for the stack.

Use a new, local variable for the loops. Make sure you declare it in the loop as well:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Derek Varghese
Greenhorn

Joined: Jun 21, 2011
Posts: 3
Thank you very much Rob for pointing that out.
I really appreciate it. I also found after I read your reply that I was also pushing the wrong arrays to stack. So I swtiched them and I am able to move forward with my code.

Thanks again.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

You're welcome.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Problem with the converting Object to Double in the output
 
Similar Threads
Event Handlers:Mouse over "circles"
Smiling Face
new to java
Getting a enum based upon one of its fields
problem in creating more than one objects