Условие: стандартный ввод данных, первая строка - количество вводов (гарантированно что их не меньше двух), каждая следующая содержит две цифры: первая цифра - идентификатор задания; вторая - уровень загрузки, цифры разделены пробелом. Необходимо разделить и записать задания в два Queue массива согласно условию - задание добавляется в тот массив, загрузка которого меньше, если загрузка одинаковая, то задание добавляется в первый массив. Вывести получившиеся массивы. Мой код
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 + " ");
}
}
}
Вопрос - почему при запуске программы не спрашивает ввод строки в цикле (строка с коментом "//start"), а сразу кидает на добавление зачения в массив first и как следствие
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
а если заменить код между //start и //end на закоментированный участок кода, то все норм...
String s = "5 6"; System.out.print(s.split(" ").length);вернет 2. – not a Programmer Apr 01 '18 at 09:34