Second number not getting into 2D array
I'm kinda puzzled here as to why the second number the user inputs is not getting into the array.
I know my code is poor, but I'm just getting to know java and it doesn't like me.
Code:
private void enterButtonActionPerformed(java.awt.event.ActionEvent evt) {
// Define variables
double minutesIn = 0; double paymentIn = 0;
//Get input (minutes and payment) from user
try{
minutesIn = Double.parseDouble(minutesField.getText());
paymentIn = Double.parseDouble(paymentField.getText());
if(minutesIn>=0);
else{
outputTextArea.append("Please enter a vaild number of minutes\n");
return;
}
if(paymentIn>=0);
else{
outputTextArea.append("Please enter a vaild payment amount\n");
return;
}
}
catch(Exception e)
{
outputTextArea.append("Please input a numerical value\n");
return;
}
// Store input into array
double[][] userInput = {{minutesIn},{paymentIn}};
System.out.println(userInput[0][0] + "\t");
System.out.println(userInput[0][1]);
The last 2 System.out.println lines are just for me to see what the values are in the console, for debugging purposes.
The first value is fine, I can see whatever I enter. The second value produces an ArrayIndexOutOfBoundsException.
Not sure why as I have declared the array to be 2D.
Re: Second number not getting into 2D array
The error on your code is that you are accessing the array outside it range. You have an array named userInput which length is 2. The first element is another array with one element. The second element is also an array with one element. If you need to take out the minuteIn you should access it using userInput[0][0]. And to access the paymentIn your indexes are userInput[1][0] not userInput[0][1].
Re: Second number not getting into 2D array
Quote:
Originally Posted by
wsaryada
The error on your code is that you are accessing the array outside it range. You have an array named userInput which length is 2. The first element is another array with one element. The second element is also an array with one element. If you need to take out the minuteIn you should access it using userInput[0][0]. And to access the paymentIn your indexes are userInput[1][0] not userInput[0][1].
I just tried that and you're right! Thanks.
Follow up question - I thought that 2D arrays were stored [row][column]
Maybe I've declared the array incorrectly. I'm trying to get 2 columns to add up later on. The first being minutesIn and the second being paymentIn.
It looks like all the input is going to the first column or am I way off here somehow.
Re: Second number not getting into 2D array
Well, if you considering a 2D array as in row and column format try to picture it this way:
Code:
double[][] userInput = {
{minutesIn}, // <- your first row, contains a single column
{paymentIn} // <- your second row, contains a single column
};
Re: Second number not getting into 2D array
So my declaration statement is wrong for what I'm trying to do.
This is what I have pictured in my head
| column 1 | column 2 |
|-----------|-----------|
| minutesIn | paymentIn |
How would I do that?
Re: Second number not getting into 2D array
A one dimensional array of double will do, unless you envision multiple rows. If so, then you'd want:
{ {row1Item1, row1item2}, {row2Item1, row2Item2}}
Re: Second number not getting into 2D array
Thank you Fubarable!
Just had a couple of extra curly brackets. :)
Oh, I was after multiple rows.