I made a program where when the user selects something from the JComboBox a value will be placed in the JTextFields along with TOTAL and the user will input any number in the POINTS field and the product of the values in the EARNING field next to each of the TOTAL & POINTS fields will show.
But here only the product of the last two adjacent fields is shown in each of the EARNING fields.
I want:
200 * 1 = 200
100 * 2 = 200
200 * 4 = 800
100 * 6 = 600
But it always shows 600 the last product of two fields in each EARNING fields.
Here a Screenshot is attached for better understanding.
My Code for counting multiplication is given below:
private void calcCredit() {
try {
double summation = 1;
int i = 0;
for (JTextField textField : arr) {
for(JTextField fields : arr1.subList(i, arr1.size())) {
summation = Double.parseDouble(textField.getText()) * Double.parseDouble(fields.getText());
}
i ;
}
for(JTextField f : arr2) {
f.setText(String.valueOf(summation));
}
} catch (Exception ex) {
}
}
Here all the JTextFields under TOTAL are added in an ArrayList named arr, all the JTextFields under POINTS are added in an ArrayList named arr1 and the rest fields are added in ArrayList named arr2
CodePudding user response:
I see that your code has the variable i and you increment i in the outer loop. I think you were thinking of using i as index to the arrays. And this would be the right direction.
The solution is to use i to refer to the row of the three fields. For example, arr[i], arr1[i], arr2[i] would refers to all the three fields on row i. You need to compute the earning for each row and update the earning field for that row.
private void calcCredit() {
for(int i = 0; i<4; i ){
double total = Double.parseDouble(arr[i].getText());
double points = Double.parseDouble(arr1[i].getText());
double earning = total * points;
arr2[i].setText("" earning);
}
}

