Store Values in 2D array and output to textarea
I have a java GUI that contains 2 text boxes and 2 buttons. When the enter button is pushed it needs to store the values in the text boxes in a 2D array and clear the text boxes. When the report button is pushed it needs to output all values in the 2D array to a text area. Below is the code that I have so far. It appears that it is not adding all the values to the rows as it loops and I would like to know how to ouput all values in the array to a textarea. Any help would be greatly appreciated. Thank You
Code :
public class TutoringSessionUI extends javax.swing.JFrame {
double [][] tutorArray = new double[1][2];
private void EnterButtonActionPerformed(java.awt.event.ActionEvent evt) {
for(int j = 0; j < tutorArray.length; j++){
for(int k = 0; k < tutorArray.length; k++){
//double minutesInt = 0;
//double moneyInt = 0;
double minutesInt = Double.parseDouble(TimeText.getText());
double moneyInt= Double.parseDouble(MoneyText.getText());
if (minutesInt < 0)
{
IllegalArgumentException exception
= new IllegalArgumentException("Invalid payment amount.");
throw exception;
}
//Checks for negative numbers in Money and throws exception if found.
if (moneyInt < 0)
{
IllegalArgumentException exception
= new IllegalArgumentException("Invalid time amount.");
throw exception;
}
tutorArray[j][0] = minutesInt;
tutorArray[j][1] = moneyInt;
System.out.println(tutorArray[j][0]);
System.out.println(tutorArray[j][1]);
System.out.println();
TimeText.setText("");
MoneyText.setText("");
}
}
}
private void QuitButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
System.exit(0);
}
private void ReportButtonActionPerformed(java.awt.event.ActionEvent evt) {
double tutorOutput = tutorArray[0][0] + tutorArray[0][1];
String tutorOutPut = Double.toString(tutorOutput);
ReportArea.append(tutorOutPut);
}
Re: Store Values in 2D array and output to textarea
When posting code, it should be in the form of an SSCCE. People aren't going to want to read through all that yucky GUI-editor code.
You should put some print statements inside your nested for loops (or better yet, use a debugger) to see what values j and k are reaching. Compare that to what values should they be reaching.
Re: Store Values in 2D array and output to textarea
Thanks for the quick response Kevin and I have edited the code to only include the relevant parts. Sorry for the oversight. I have placed print lines in the loops and it shows values for j and k always as 0 which is my issue. It never holds the value of j or k anywhere for the next event to add to that number.
Re: Store Values in 2D array and output to textarea
Quote:
Originally Posted by
raverz1tildawn
Thanks for the quick response Kevin and I have edited the code to only include the relevant parts. Sorry for the oversight. I have placed print lines in the loops and it shows values for j and k always as 0 which is my issue. It never holds the value of j or k anywhere for the next event to add to that number.
Thanks, although technically an SSCCE should be executable.
Your problem is in the values to which you're limiting your inner for loop. What is tutorArray.length? What do you want the loop's upper limit to be?
Since a 2D array is just an array of arrays, you can do stuff like this:
Code :
tutorArray[j] = new int[]{2, 3, 4};
It also means you can do something like this:
Re: Store Values in 2D array and output to textarea
The end result is that i have a 2D array that 2 columns and unlimited rows. Every time the enter button is pushed it stores the values in the 2 text fields in the array. When the report button is pushed it outputs all the values in the array to a textarea.