What am I doing incorrect here? cant figure out!
package com.codegym.task.task09.task0930;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
/*
Task about algorithms
*/
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<String>();
ArrayList<Integer> numbers = new ArrayList<Integer>();
//separating string and numbers to different lists I created above
for (String s : array) {
if (isNumber(s)) {
numbers.add(Integer.parseInt(s));
} else
words.add(s);
}
//sorting and reversing numbers list.
Collections.sort(numbers);
Collections.reverse(numbers);
//converting words list back to another array and validating and sorting using the isGreaterThan method.
String[] arrWords = words.toArray(new String[words.size()]);
for (int i = arrWords.length; i >= 0; i--) {
for (int j = 1; i < i; i++) {
String s = "";
if (isGreaterThan(arrWords[j - 1], arrWords[j]))
{
s = arrWords[j];
arrWords[j] = arrWords[j - 1];
arrWords[j - 1] = s;
}
}
}
//filling the original array with the required answer.
for (int i = 0; i < array.length; i++) {
if (isNumber(array[i]))
array[i] = String.valueOf(numbers.get(i));
else
array[i] = arrWords[i];
}
}
// String comparison method: 'a' is greater than 'b'
public static boolean isGreaterThan(String a, String b) {
return a.compareTo(b) > 0;
}
// Is the passed string a number?
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 == '-') // The string contains a hyphen
|| (!Character.isDigit(c) && c != '-') // or is not a number and doesn't start with a hyphen
|| (i == 0 && c == '-' && chars.length == 1)) // or is a single hyphen
{
return false;
}
}
return true;
}
}