I'm trying to make a program which allows a user to insert marks into an array and sort them. How do I do this?
private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {
String newMark;
newMark = userText.getText().substring(0, 1).toUpperCase() + userText.getText().substring(1);
if (Integer.parseInt(newMark) <= 100 && Integer.parseInt(newMark) >= 0) {
studentMarks = new ArrayList<Integer>();
studentMarks.add(Integer.parseInt(newMark));
markDisplay.append(studentMarks +"\n");
} else {
markDisplay.append("Only marks between \n 0 and 100 are accepted. \n");
}
}
private void sortButtonActionPerformed(java.awt.event.ActionEvent evt) {
studentMarksSorted = new ArrayList<Integer>();
studentMarksSorted = studentMarks;
Collections.sort(studentMarksSorted);
markDisplay.setText(studentMarksSorted + "\n");
}
I expected the output to be the marks the user inputted in descending numeric order, replacing the original unsorted list, but instead, the output was just the last number that the user added to the array.