This question has already been answered:
Condition: standard data entry, the first line is the number of entries (it is guaranteed that there are at least two), each next one contains two digits: the first digit is the task identifier; the second is the load level, the digits are separated by a space. It is necessary to divide and write tasks in two Queue arrays according to the condition - the task is added to the array whose load is less, if the load is the same, then the task is added to the first array. Print the resulting arrays. My code
import java.util.*; class Test { public static void main(String[] args) { Scanner in = new Scanner(System.in); Queue<Integer> first = new ArrayDeque<>(); Queue<Integer> second = new ArrayDeque<>(); int firstLoad = 0; int secondLoad = 0; int count = 0; int quantity = in.nextInt(); while(true) { if(count==quantity) { break; }else { String temp = in.nextLine(); //start String[] tempArr = temp.split(" "); if (firstLoad<=secondLoad){ first.add(Integer.parseInt(tempArr[0])); firstLoad = firstLoad + Integer.parseInt(tempArr[1]); count++; } else { second.add((Integer.parseInt(tempArr[0]))); secondLoad = secondLoad + Integer.parseInt(tempArr[1]); count++; //end } // int[] tempArr = new int[2]; // tempArr[0] = in.nextInt(); // tempArr[1] = in.nextInt(); // if (firstLoad<=secondLoad){ // first.add((tempArr[0])); // firstLoad = firstLoad + (tempArr[1]); // count++; // } else { // second.add(((tempArr[0]))); // secondLoad = secondLoad + (tempArr[1]); // count++; } } for (Integer i : first){ System.out.print(i + " "); } System.out.println(); for (Integer i : second) { System.out.print(i + " "); } } } The question is why, when starting the program, it does not ask for the input of a string in a loop (the string with the comment "// start"), but immediately throws an addition into the first array and as a result
Exception in thread "java.lang.NumberFormatException: For input string:"
and if you replace the code between // start and // end with the commented code section, then all the rules ...