I have to code an assignment for a college course. Long story short, part of the assignment requires me to take information from two textfields (variable names payment and time) and then enter them into a multidimensional array every time someone uses the enter button on a GUI. Then, after entering in all their payment and time amounts the user will press the "Run Report" button which will take information from the 2D array and list the information followed by some calculations like average, totals for time and payment, etc. I'm pretty much good to go on everything I think except this 2D array thing. The GUI is built and I'm trying to build functionality for the "Enter" button. I'm not real sure how to properly implement this though. Here's what I have so far.
I first declared some global variables and the array:
public class TutorUI extends javax.swing.JFrame { double[][] ptArray = new double[20][2]; double i = 0; double j = 0;
Then added some coding for the enter button:
private void enterActionPerformed(java.awt.event.ActionEvent evt) { /*gets the value from the two text fields and sets them equal to the variables*/ double paymentValue = Double.parseDouble(payment.getText()); double timeValue = Double.parseDouble(time.getText()); //Checks for negative numbers and throws exception if found. if (paymentValue < 0) { IllegalArgumentException exception = new IllegalArgumentException("Invalid payment amount."); throw exception; } //Checks for negative numbers and throws exception if found. if (timeValue < 0) { IllegalArgumentException exception = new IllegalArgumentException("Invalid time amount."); throw exception; } //Sets the variables to column 1 and column 2. ptArray[0][0] = timeValue; ptArray[0][1] = paymentValue;
Basically I want to set the timeValue to column 1 and paymentValue to column 2 and then the array just adds a new row each time someone enters new data. I feel like maybe I'm going about this in the wrong way since when I try to test the array by appending the data to the textarea it spits out gibberish. I've tried about 20 different ways to get this to work and this is just one of the many. Any help, tips, links to tutorials, etc. will be greatly appreciated. Thanks.