-1

In my class, we need to create a program where you can add students marks and then sort them by size.

"Sort the marks array in ascending order and display the sorted array in a JTextArea."

 //(This is from a private void for the "sort" button)

 // declaring variables
    int intValuesMarks;


    // assigning values to variables
    intValuesMarks = Integer.parseInt(AllMarks.getText());
    ArrayList<Integer>arrValueList = new ArrayList<Integer>();
    arrValueList.add(intValuesMarks);
    Collections.sort(arrValueList);
    AllMarks.setText("" + arrValueList);

When I try to run it I get this error:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "
 14
 14
 21
 42
 12"

(The numbers above being the numbers I inputted before to try the program)

Help!

Thank you.

  • 1
    You are attempting to parse a list of numbers into a single `int`. Separate the numbers and parse each element individually. Check [here](http://stackoverflow.com/questions/225337/how-do-i-split-a-string-with-any-whitespace-chars-as-delimiters) for splitting a string using whitespace as the delimeter. Once they're split, you may loop over each item. – CubeJockey Jul 21 '15 at 17:08
  • That is because "14 14 21 42 12" is not a number. You have to parse them seperately. You could e.g. split the `String` first at the blanks and then parse every `String` separately. – Turing85 Jul 21 '15 at 17:09

3 Answers3

1
String str[]=AllMarks.getText().split("\\s+");//groups all white spaces as a delimiter

here you are getting string like 14 14 21 42 12. so it may not parse to int. parse each one separately by splitting this by using String#split(String)

ArrayList<Integer>arrValueList = new ArrayList<Integer>();

for(int i=0;i<str.length;i++)
{
    intValuesMarks = Integer.parseInt(str[i]);

    arrValueList.add(intValuesMarks);
}

Collections.sort(arrValueList);//now sort your list here.
AllMarks.setText("" + arrValueList);
CubeJockey
  • 2,209
  • 8
  • 24
  • 31
SatyaTNV
  • 4,137
  • 3
  • 15
  • 31
  • A code-only answer is never good. It would be appreciated if you explain your solution. – Turing85 Jul 21 '15 at 17:11
  • 1
    It is best to use `"\\s+"` as your regex. See [here](http://stackoverflow.com/questions/225337/how-do-i-split-a-string-with-any-whitespace-chars-as-delimiters) for more detail. – CubeJockey Jul 21 '15 at 17:13
1

Your AllMarks.getText() is actually returning all the marks as a single string seperated with space.You were trying to parse that to an integer which is why you got a number format exception.

int intValuesMarks;
        ArrayList<Integer>arrValueList = new ArrayList<Integer>();
        String str[]=AllMarks.getText().split("\\s+");//created a string array , each element in the array is the marks.
        for (String string : str) {
            intValuesMarks = Integer.parseInt(string);
            arrValueList.add(intValuesMarks);


        }

         Collections.sort(arrValueList);
         AllMarks.setText("" + arrValueList);
Jishnu Prathap
  • 1,955
  • 2
  • 21
  • 37
  • It is best to use `"\\s+"` as your regex. See [here](http://stackoverflow.com/questions/225337/how-do-i-split-a-string-with-any-whitespace-chars-as-delimiters) for more detail. – CubeJockey Jul 21 '15 at 17:14
  • 1
    @JishnuPrathap `intValuesMarks = Integer.parseInt(str);` Returns an error on `"str"` _Incompatible types String[] can not be converted to String_ – Faizaan Chishtie Jul 21 '15 at 17:28
0

You probably already know this line is your problem:

intValuesMarks = Integer.parseInt(AllMarks.getText());

AllMarks.getText() is returning a list of integers, one per line. You need to parse that line first into a list or array of Strings, then parseInt each one of them to get individual integers. You have a couple of options; one is the following:

String[] stringValuesMarks = AllMarks.getText().split("\n");

Then iterate over stringValuesMarks to get the individual integers.

Jerry Andrews
  • 847
  • 5
  • 23
  • 1
    It is best to use `"\\s+"` as your regex. See [here](http://stackoverflow.com/questions/225337/how-do-i-split-a-string-with-any-whitespace-chars-as-delimiters) for more detail. – CubeJockey Jul 21 '15 at 17:14
  • cool. I'm leaving the original text so others will learn the same thing *I* just learned. – Jerry Andrews Jul 21 '15 at 17:46