I don't know what's causing the nullpointerexception.
package com.codegym.task.task09.task0930;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.*;
/*
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) {
// write your code here
ArrayList<Integer> numbers = new ArrayList<Integer>();
ArrayList<String> originalString = new ArrayList<String>();
ArrayList<String> newList = new ArrayList<>();
int length = array.length;
for(int i = 0; i < length; i++) {
if(isNumber(array[i]) == true) {
numbers.add(Integer.parseInt(array[i]));
} else {
originalString.add(array[i]);
}
}
// Sort the numbers in descending order in the number-based array
Collections.sort(numbers, Collections.reverseOrder());
// Sort the strings in ascending order using the isGreaterThan method
for(int i = 0; i < originalString.size(); i+=2) {
String first = originalString.get(i-1);
String second = originalString.get(i);
if(!isGreaterThan(first, second)) {
originalString.set(i, second);
originalString.set(i+1, first);
}
}
// Collections.sort(originalString);
for(int i = 0; i < ((array.length)/2); i++) {
/*System.out.println(newList.get(i));
System.out.println(numbers.get(i));*/
newList.add(originalString.get(i));
newList.add(numbers.get(i).toString());
}
for(int i = 0; i < array.length; i++) {
System.out.println(newList.get(i));
array[i] = newList.get(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;
}
}