I got my algorithm to sort and display the results as shown in the output example, but it's not recognized by system. Can anyone advise as to why it's not working? Thank you.
package com.codegym.task.task09.task0930;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.*;
import java.io.*;
/*
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>();
Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
String s = scan.nextLine();
// 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) {
// write your code here
ArrayList<Integer> numbers = new ArrayList<Integer>();
ArrayList<String> originalString = new ArrayList<String>();
ArrayList<String> newString = new ArrayList<String>();
ArrayList<String> newList = new ArrayList<String>();
ArrayList<Integer> order = new ArrayList<Integer>();
int length = array.length;
for(int i = 0; i < length; i++) {
if(isNumber(array[i])) {
numbers.add(Integer.parseInt(array[i]));
order.add(1);
} else {
originalString.add(array[i]);
order.add(0);
}
}
// Sort the numbers in descending order in the number-based array
Collections.sort(numbers);
Collections.reverse(numbers);
for(Integer num : numbers) {
System.out.println(num.toString());
}
// Sort the strings in ascending order using the isGreaterThan method
int spaces = originalString.size();
String[] placeholder = new String[spaces];
int counter;
for(String item : originalString) {
counter = 0;
for(int i = 0; i < originalString.size(); i++) {
if(isGreaterThan(item, originalString.get(i))) {
counter++;
}
}
placeholder[spaces-counter-1] = item;
}
for(int i = 0; i < placeholder.length; i++) {
newString.add(placeholder[i]);
}
Collections.reverse(newString);
for(String word : newString) {
System.out.println(word);
}
int wordIndex = 0;
int numIndex = 0;
for(int num : order) {
if(num == 0) {
newList.add(newString.get(wordIndex));
wordIndex++;
} else {
newList.add(numbers.get(numIndex).toString());
numIndex++;
}
}
for(int i = 0; i < newList.size(); i++) {
System.out.println(newList.get(i));
array[i] = newList.get(i);
}
for(String position : array) {
System.out.println(position);
}
}
// 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;
}
}