• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with the converting Object to Double in the output

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you are going to iterate through an array, you are usually better off to use the length:

 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.
reply
    Bookmark Topic Watch Topic
  • New Topic