-2
import java.util.*;
public class MagicSum {
public static void main(String[] args) throws Exception{
    Scanner scan = new Scanner(System.in);
    int size = scan.nextInt();

    List<Integer>  list = new ArrayList<>();
    String numberString = scan.nextLine();
    String[] numberArray = numberString.trim().split(" ");
    for(int i=0;i<numberArray.length;i++){
        Integer num = Integer.parseInt(numberArray[i]);
        list.add(num);
    }
   /* for(int i=0;i<size;i++){
      int num = scan.nextInt();
      list.add(num);
    }*/

    List<Integer> sortList = new ArrayList<>(list);
    Collections.sort(sortList);
    System.out.println(list);
    System.out.println(sortList);
    int good=0,bad=0;
    for(int i=0;i<list.size();i++){
        if(list.get(i) == sortList.get(i)){
            good+=list.get(i);
        }
        else{
            bad+=list.get(i);//0 0 0 0 1 1 2 3 4 4
                             //0 4 0 0 1 3 4 1 0 2
        }
        System.out.println(String.format("good = %d  bad = %d",good,bad));
    }
    System.out.println(String.format("Final good = %d  bad = %d",good,bad));
    System.out.println(good-bad);
}
}

Exception in thread "main" java.lang.NumberFormatException: For input string: "" at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67) at java.base/java.lang.Integer.parseInt(Integer.java:678) at java.base/java.lang.Integer.parseInt(Integer.java:786) at MagicSum.main(MagicSum.java:12)

jps
  • 15,760
  • 14
  • 59
  • 71
  • Why do you think this should *not* be raising an exception? What is this supposed to do? – Scott Hunter May 26 '22 at 14:45
  • `Integer.parseInt(numberArray[i])` tries to parse an empty string. `numberArray[i]` contains an empty string. That means your `numberString.trim().split(" ")` expression returns an array with empty strings as elements. – knittl May 26 '22 at 14:45
  • You should call `#nextLine` after your first `#nextInt` call, as you are pulling the remainder of the empty line when you get the size. – Rogue May 26 '22 at 15:51

0 Answers0