-3

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.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • This is very odd-looking Javascript, can you tell me which version you're using so I can research it? – Snow May 26 '19 at 23:31
  • I believe it's Java SE, however, I'm not certain of more than that, it's just the default on Netbeans IDE 8.2. – Zainou 86 May 26 '19 at 23:34
  • 1
    It's Java, not JavaScript (as @Snow already knows) , and I've removed the [tag:javascript] tag, but if you need a more complete answer, please consider creating a valid [mre], code we can run and test. Note that we are not asking for the entire program, just a small program that demonstrates the problem. – Hovercraft Full Of Eels May 26 '19 at 23:35
  • This question was originally closed as a duplicate of: https://stackoverflow.com/questions/8938235/sort-an-array-in-java, apparently based on the question title asking how to sort an array in ascending order. The answer given in that posting all deal with sorting the array once the data has been added. However the OP is aware of the Colloections.sort(...), so I believe the actual problem is how the OP is attempting to add the data to the ArrayList. So I reopened the question to address this issue. – camickr May 26 '19 at 23:54

1 Answers1

0

but instead the output was just the last number that the user added to the array.

studentMarks = new ArrayList<Integer>();
studentMarks.add(Integer.parseInt(newMark));

Well, you create a new ArrayList every time you add a mark.

So of course you only have a single number.

The ArrayList should be created once in the constructor of your class and then you just add new Integer values to the ArrayList.

studentMarksSorted = new ArrayList<Integer>();
studentMarksSorted = studentMarks;

And there is no need for doing the above. That does not copy the data from one ArrayList to another. If you want to keep track of the original marks and the sorted marks, then you need to manually copy the Integers from one ArrayList to the other.

camickr
  • 321,443
  • 19
  • 166
  • 288