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 ...

Reported as a duplicate by zRrr members, Community Spirit Apr 1 '18 at 19:13 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

    2 answers 2

    Because in your code you use Integer.parseInt() which throws an exception if the parameter is not a number. And so it is if you look at the message exceptions. The fact is that after the split, an array element with an empty value appears. You need to either exclude this element from the loop or catch the exception in a separate block.

    • read the question again, after the split an empty array appears because the standard input of the string is not called (if it had volunteered, Integer.parseInt () would convert the element of the tempArr array to a number) - Maksim Dzianisik
    • @RomanC, and that's not true. No empty item appears, specially checked. The following code is String s = "5 6"; System.out.print(s.split(" ").length); String s = "5 6"; System.out.print(s.split(" ").length); will return 2. - not a Programmer
    • @Maksim Not correct, the standard input is just called but the reading is blocked. - Roman C
    • @not The code you provided has nothing to do with the problem indicated in the question, but the attacks to my answer. - Roman C
    • @RomanC, how does it not have? I say that your statement: The fact is that after the split an element of the array appears with a null value - is incorrect and I give an example that confirms this. - not a Programmer

    You do not need to break the line into spaces, as the Scanner does this by default. Here's how to proceed:

     List<Integer> values = new ArrayList<>(); Scanner scanner = null; try { scanner = new Scanner(new BufferedReader(new InputStreamReader(System.in))); while(scanner.hasNext()) { values.add(Integer.valueOf(scanner.next())); } } finally { if(scanner != null) scanner.close(); } 

    Here I did not consider two points:

    1. The user can enter not a number;
    2. It is necessary to determine how to finish the input (otherwise the program will constantly wait for input).

    Update: Now the program takes into account that the user can enter not a number. Made two lists (like yours) that are filled in alternately.

     List<Integer> first = new ArrayList<>(); List<Integer> second = new ArrayList<>(); boolean flag = true; try(Scanner scanner = new Scanner(new BufferedReader(new InputStreamReader(System.in)))) { while(scanner.hasNext()) { String s = scanner.next(); if(!s.matches("-?\\d+(\\.\\d+)?")) continue; if(flag) { first.add(Integer.valueOf(s)); } else { second.add(Integer.valueOf(s)); } flag = !flag; } }