As part of my homework I have the following code:
ArrayList <Integer> marks = new ArrayList();
Collections.addAll(marks, (Integer.parseInt(markInput.getText())));
private void analyzeButtonActionPerformed(java.awt.event.ActionEvent evt)
{
analyzeTextArea.setText("Number at level R:" + calculateLevelR());
}
private int calculateLevelR() {
int sum = 0;
for (Integer mark: marks) {
if(mark < 50)
sum += mark;
sum = marks.size();
}
return sum;
}
When a user enters a set of grades, I’m trying to calculate and display the amount of grades that are “level R” (under 50) but the above code does not work properly, instead it calculates the size of the whole array.
How can I get it to calculate just the amount of grades at “level R”?
Other answers have already pointed out the exact problem, looking at your comments may be a code snippet can explain better. See below
What you need is :