0

I'm having issues retrieving values from a "2d array" in Java. I know there isn't really such thing as a 2d array in Java, but bear with me. I am very new to Java, and am working on a project for my class. I'm supposed to be submitting information from a jTextField to an array, then pulling the information stored in the array back up for a summary report. Here's what I have so far:

public class KETTask1UI extends javax.swing.JFrame {
    //Creates the "2d array" to store minutes worked and payment values
    int[][] paymentArray = new int [20][2];


    /**
     * Creates new form KETTask1UI
     */
    public KETTask1UI() {
        initComponents();
    }


    private void runreportActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        for (int i=0; i<paymentArray.length; i++) {
            for (int j=0; j<paymentArray[i].length; j++) {
        //Blanks out the displayTextArea
        displayTextArea.setText(" ");
        //Displays the contents of the array in the displayTextArea
        displayTextArea.append("History of minutes and payment entered:\n");
        displayTextArea.append(String.valueOf(paymentArray[i][j]));
        }   
        }
    }                                         

    private void minutesActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
    }                                       

    private void quitActionPerformed(java.awt.event.ActionEvent evt) {                                     
        System.exit(0);
    }                                    

    private void enterButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:


 int min = Integer.parseInt(minutes.getText());
int pay = Integer.parseInt(payment.getText());
for (int i=0; i<paymentArray.length; i++) {
     for (int j=0; j<paymentArray[i].length; j++) {
        //Sets the amount of minutes worked and payment to the array
        paymentArray[i][0] = min;
        paymentArray[i][1] = pay;
        //display a message to the user to let them know the values that they've entered.
        displayTextArea.append("******************\nRaw Tutoring Earnings Data\n\nMinutes     Earnings\n");
        displayTextArea.append(String.valueOf(paymentArray[i][j]));
        displayTextArea.append("\n\n******************");
                }
            }

    }


    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new KETTask1UI().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JTextArea displayTextArea;
    private javax.swing.JButton enterButton;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField minutes;
    private javax.swing.JTextField payment;
    private javax.swing.JLabel paymentLabel;
    private javax.swing.JButton quit;
    private javax.swing.JButton runreport;
    private javax.swing.JLabel timeLabel;
    // End of variables declaration                   
}

Now, currently when I click enter button, all it gives me is the following (30 being the last number of minutes and 20 being the last number for payment):

******************
Raw Tutoring Earnings Data

Minutes     Earnings
30

************************************
Raw Tutoring Earnings Data

Minutes     Earnings
20

This message repeats 20 times in the textArea. If I enter more data, it just does the same thing with the new numbers.

I need it to look like this:

******************
Raw Tutoring Earnings Data

Minutes     Earnings
20          30

******************

When I enter new values and push the enter button again, I need it to display an updated list like this:

******************
Raw Tutoring Earnings Data

Minutes     Earnings
20          30
30          15

******************
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
  • 1
    What's the output vs expected output? – Sterling Archer Aug 26 '13 at 02:14
  • For what it's worth, this is a situation where the "nice" way to do this is to have an array of class-type objects. The "row" here isn't really a list of different occurrences of the same thing. [i][0] is minutes, [i][1] is payment, and [i][2] if it ever existed would presumably be something else. – Mike Housky Aug 26 '13 at 02:24
  • The error looks like a different problem from the 2D array issue, can you post what's happening at KETTask1UI (line 47)? You're getting an error in the actionPerformed method. It could also be NetBeans, check http://stackoverflow.com/questions/4386076/uncompilable-source-code-runtimeexception-in-netbeans – hotforfeature Aug 26 '13 at 15:58
  • Ok, I've figured out my compiling issue, and now my app will compile. However, the output that I'm getting is not the desired result. I think it has something to do with my loop counter being inside the button code... I'm going to edit my original post with my full code. Hopefully there is something I'm blatantly doing wrong. – David Cooney Aug 28 '13 at 02:25

2 Answers2

2

2D arrays do exist in Java, and you're using them correctly. However, in your second code block, you're using the paymentArray's first dimension's length where you loop through with 'j'. Use this to access the second dimension's length.

for (int i = 0; i < paymentArray.length; i++) {
    for (int j = 0; j <paymentArray[i].length; j++) {
        displayTextArea.append(String.valueOf(paymentArray[i][j]));
    }
}
hotforfeature
  • 2,558
  • 1
  • 16
  • 24
  • 1
    I would either go with `paymentArray[i].length` or store `paymentArray[0].length` in a variable, to avoid accessing costs. Just a suggestion. – Sinkingpoint Aug 26 '13 at 02:17
  • It's also important to use i rather than zero as an index since in theory each subarray can have a different length. – Ernest Friedman-Hill Aug 26 '13 at 02:21
  • Good point, using i would be much better for different dynamic array lengths, I edited my answer – hotforfeature Aug 26 '13 at 02:25
  • I've made the recommended changes, but I still do not get the desired results. If I run it from within NetBeans, it will run, but when I click the button to dump the array into the textArea, I don't get anything in the textArea. I'll edit my main post with the error it's giving me. – David Cooney Aug 26 '13 at 02:39
0

Your code is little incorrect. Change it to below

for (int i = 0; i < paymentArray.length; i++) {
    for (int j = 0; j <paymentArray[i].length; j++) {
        displayTextArea.append(String.valueOf(paymentArray[i][j]));
    }
}
H-Patel
  • 98
  • 5