I'm constantly getting a NullPointer Exception error. I can't get it to compile to see what I need to change. Please assist. Thanks.
package com.codegym.task.task09.task0930;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
/*
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<String> words = new ArrayList<>();
ArrayList<Integer> num = new ArrayList<>();
for(String s: array) {
if(isNumber(s)) {
num.add(Integer.parseInt(s));
}
else {
words.add(s);
}
}
//sort numbers
for(int i = 0; i < num.size(); i++) {
for(int j = i+1; j < num.size()-1; j++) {
if(isGreaterThan(num.get(i), num.get(j))) {
int temp = num.get(i);
num.set(i, num.get(j));
num.set(j, temp);
}
}
}
//sort words
for(int i = 0; i < words.size(); i++) {
for(int j = i+1; j < words.size()-1; j++) {
if(words.get(i).compareTo(words.get(j)) > 0) {
String temp = words.get(i);
words.set(i, words.get(j));
words.set(j, temp);
}
}
}
//add new values to orignal array
for(int i = 0; i < array.length; i++) {
int w = 0, n = 0;
if(isNumber(array[i])) {
array[i] = num.get(n).toString();
n++;
}
else {
array[i] = words.get(w);
w++;
}
}
}
// String comparison method: 'a' is greater than 'b'
public static boolean isGreaterThan(int a, int b) {
return a < b;
}
// 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;
}
}