import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; /* Задача по алгоритмам Задача: Пользователь вводит с клавиатуры список слов (и чисел). Слова вывести в возрастающем порядке, числа - в убывающем. Пример ввода: Вишня 1 Боб 3 Яблоко 2 0 Арбуз Пример вывода: Арбуз 3 Боб 2 Вишня 1 0 Яблоко */ public class Solution { public static void main(String[] args) throws Exception { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); ArrayList<String> list = new ArrayList<String>(); while (true) { String s = reader.readLine(); if (s.isEmpty()) break; list.add(s); } String[] array = list.toArray(new String[list.size()]); sort(array); for (String x : array) { System.out.println(x); } } public static void sort(String[] array) { //напишите тут ваш код ArrayList<String> words = new ArrayList<>(); ArrayList<Integer> numbers = new ArrayList<>(); ArrayList<Boolean> bools = new ArrayList<>(); for (String a : array) { if (isNumber(a)) { bools.add(true); numbers.add(Integer.parseInt(a)); } else { bools.add(false); words.add(a); } } for (int i = 0; i < words.size() - 1; i++) { for (int j = 0; j < words.size() - i - 1; j++) { if (isGreaterThan(words.get(j + 1), words.get(j))) { String change = words.get(j + 1); words.set(j + 1, words.get(j)); words.set(j, words.get(j + 1)); } } } for (int i = 0; i < numbers.size() - 1; i++) { for (int j = 0; j < numbers.size() - i - 1; j++) { if (numbers.get(j) < numbers.get(j + 1)) { int change = numbers.get(j); numbers.set(j, numbers.get(j + 1)); numbers.set(j + 1, change); } } } for (int i = 0, ii = 0, check = 0; check < array.length; ) { if (bools.get(i)) { array[check] = String.valueOf(numbers.get(i)); i++; check++; } if (!(bools.get(ii))) { array[check] = words.get(ii); ii++; check++; } } } //Метод для сравнения строк: 'а' больше чем 'b' public static boolean isGreaterThan(String a, String b) { return a.compareTo(b) > 0; } //строка - это на самом деле число? public static boolean isNumber(String s) { if (s.length() == 0) return false; char[] chars = s.toCharArray(); for (int i = 0; i < chars.length; i++) { char c = chars[i]; if ((i != 0 && c == '-') //есть '-' внутри строки || (!Character.isDigit(c) && c != '-') ) // не цифра и не начинается с '-' { return false; } } return true; } } 

That is the task. The problem is that for some reason I have input without stopping, even when I enter an empty string. I didn’t find a problem in the code, please help) And yes, don’t write why I didn’t sort by ready-made methods, I don’t take the task.

  • and yes, I know that it was possible to sort in 1 cycle, I repeated to memorize the algorithm of puryl sorting. - Maxgmer
  • For a start, at least look at which line indexOutOfBoundsException occurs - Gikas

1 answer 1

You have an invalid merge algorithm.

You check bools.get(i) , that is, the index among the lines. If, for example, bools.get(i) contains false , you will never increase i , and go through the entire cycle trying to read the values ​​from words (and there they may well be less than the total length of the source container array.length ).

You need to check the overall index, in your case it is just i + ii .

In addition, the check i < array.length - 1 && ii < array.length - 1 incorrect. You need either i < numbers.length || ii < words.length i < numbers.length || ii < words.length , or i + ii < array.length .

(Perhaps there are other errors, trace.)

  • changed the code to a new one, a strange error appeared, the input goes on forever, although when I enter an empty line, it should stop, change nothing there. - Maxgmer
  • @Maxgmer: Well, try tracing, s.isEmpty() why s.isEmpty() not executed. - VladD
  • Damn, here the debugger works harder than in pascal, how to debug programs in Intellij IDEA? How to move along the steps, is there any report on the implementation of the program? - Maxgmer
  • @Maxgmer: It does not help: jetbrains.com/help/idea/2016.2/debugging.html ? - VladD