I'm having issues retreiving values from a "2d array" in Java. I know there isn't really such thing as a 2d array in Java, but bare 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:
我有问题从Java中的“二维数组”中检索值。我知道Java中没有二维数组这样的东西,但是对我来说是裸露的。我是Java的新手,正在为我的班级开发一个项目。我应该将jTextField中的信息提交到数组,然后将存储在数组中的信息备份到摘要报告中。这是我到目前为止所拥有的:
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):
现在,当我点击进入按钮时,它给我的全部内容如下(30表示最后一分钟,20表示最后一个付款号码):
******************
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.
此消息在textArea中重复20次。如果我输入更多数据,它就会对新数字做同样的事情。
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
******************
Any help is greatly appreciated. Thank you.
任何帮助是极大的赞赏。谢谢。
2 个解决方案
#1
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.
2D数组确实存在于Java中,您正确使用它们。但是,在您的第二个代码块中,您使用paymentArray的第一个维度的长度,其中您使用'j'循环。使用它来访问第二个维度的长度。
for (int i = 0; i < paymentArray.length; i++) {
for (int j = 0; j <paymentArray[i].length; j++) {
displayTextArea.append(String.valueOf(paymentArray[i][j]));
}
}
#2
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]));
}
}
#1
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.
2D数组确实存在于Java中,您正确使用它们。但是,在您的第二个代码块中,您使用paymentArray的第一个维度的长度,其中您使用'j'循环。使用它来访问第二个维度的长度。
for (int i = 0; i < paymentArray.length; i++) {
for (int j = 0; j <paymentArray[i].length; j++) {
displayTextArea.append(String.valueOf(paymentArray[i][j]));
}
}
#2
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]));
}
}