Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with the converting Object to Double in the output

 
Derek Varghese
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12196
35
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you are going to iterate through an array, you are usually better off to use the length:

 
Rob Spoor
Sheriff
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Derek Varghese
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic