private final String[] buttonNameArray = {"cButton", "ceButton",
"percentButton", "sqrtButton", "plusButton", "mcButton", "button7",
"button8",
"button9", "dashButton", "mrButton", "button4", "button5",
"button6",
"starButton", "mDashButton", "button1", "button2", "button3",
"slashButton",
"mPlusButton", "plusDashButton", "dotButton", "button0",
"equalButton"};
private final String[] buttonValArray = {"CE", "%", "\u221a", "+",
"MC", "7", "8", "9", "-", "MR", "4", "5", "6", "*", "M-", "1", "2",
"3", "/",
"M+", "+/-", ".", "0", "="};
private Hashtable buttonHash = null;
private void initComponents() {
this.buttonHash = new Hashtable();
for (int i = 0; i < this.buttonNameArray.length; i++) {
this.buttonHash.put(this.buttonNameArray[i],
this.buttonValArray[i]);
}
}
Output:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 24
at JCalculator.initComponents(JCalculator.java:44)
at JCalculator.<init>(JCalculator.java:32)
at JCalculator.main(JCalculator.java:66)
Java Result: 1
How on earth is this spawning an ArrayIndexOutOfBoundsException? I
don't see how that is possible as the array pointer should be at 0 and
there are a lot more than 0 elements in the final array.
Help appreciated, I am totally not understanding.
Phil
A. Bolmarcich - 11 Jan 2007 22:31 GMT
> private final String[] buttonNameArray = {"cButton", "ceButton",
> "percentButton", "sqrtButton", "plusButton", "mcButton", "button7",
[quoted text clipped - 36 lines]
>
> Help appreciated, I am totally not understanding.
The exception message is complaining an index value of 24 begin out of
bounds. What are the values of buttonNameArray.length and
buttonValArray.length?
Lew - 12 Jan 2007 04:21 GMT
phillip.s.powell wrote:
>> private final String[] buttonNameArray = {...};
>> private final String[] buttonValArray = {...};
It is hard in this type of situation to keep the arrays in synch with each
other. You could use a String [] [] to keep your pairs consistent, say,
private final String [] [] nameVals
= {{ "cButton", "CE" }, { "ceButton", "%" }, ... };
>> private Hashtable buttonHash = null;
You don't need to redundantly initialize instance variables to null.
Oh, and you might not need a Hashtable; a HashMap might serve your purposes.
It depends on multithreading and synchronization strategies.
private Map< String, String> buttonMap = new HashMap< String, String> ();
>> private void initComponents() {
>>
[quoted text clipped - 4 lines]
>> }
>> }
for ( String [] pair : nameVals )
{
buttonMap.put( pair [0], pair [1] );
}
If you didn't control the array, you'd need to watch out for pair.length < 2.
> The exception message is complaining an index value of 24 begin out of
> bounds. What are the values of buttonNameArray.length and
> buttonValArray.length?
With only one array, and a guarantee that each element thereof has two
elements, there is only one length to measure.
- Lew
piyu - 12 Jan 2007 04:49 GMT
> private final String[] buttonNameArray = {"cButton", "ceButton",
> "percentButton", "sqrtButton", "plusButton", "mcButton", "button7",
[quoted text clipped - 10 lines]
> "3", "/",
> "M+", "+/-", ".", "0", "="};
This is just a typing error dude. You do not have value defined for
cButton in buttonValArray.
Best is to use only single array to avoid such problems in future.
Regards,
Piyu.
phillip.s.powell@gmail.com - 16 Jan 2007 21:49 GMT
> > private final String[] buttonNameArray = {"cButton", "ceButton",
> > "percentButton", "sqrtButton", "plusButton", "mcButton", "button7",
[quoted text clipped - 14 lines]
> cButton in buttonValArray.
> Best is to use only single array to avoid such problems in future.
single array how? Arrays are only enumerative in Java, unless you mean
Hashtable.. ???
> Regards,
> Piyu.
Ian Shef - 16 Jan 2007 19:58 GMT
> private final String[] buttonNameArray = {"cButton", "ceButton",
> "percentButton", "sqrtButton", "plusButton", "mcButton", "button7",
[quoted text clipped - 34 lines]
> don't see how that is possible as the array pointer should be at 0 and
> there are a lot more than 0 elements in the final array.
No, the message told you that the array pointer is at 24.
This is not a problem for buttonNameArray - you used its length, so this
part has to be right.
> Help appreciated, I am totally not understanding.
However, you did not check the length of buttonValArray and thus went right
off of the end.
> Phil
Good Luck!

Signature
Ian Shef 805/F6 * These are my personal opinions
Raytheon Company * and not those of my employer.
PO Box 11337 *
Tucson, AZ 85734-1337 *