I am getting a NoSuchElementException in line 19. How should I overcome this?
package com.codegym.task.task09.task0930;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.NoSuchElementException;
/*
Task about algorithms
*/
public class Solution {
public static void main(String[] args) throws Exception, NoSuchElementException {
Scanner sc = new Scanner(System.in);
ArrayList<String> list = new ArrayList<String>();
String s;
while(!(s = sc.nextLine()).equals("")) {
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) {
Integer[] num = new Integer[array.length];
String[] word = new String[array.length];
int count1=0,count2=0;
for(String s : array) {
if(!isNumber(s)) {
word[count1]=s;
count1++;
}
else {
num[count2]=Integer.parseInt(s);
count2++;
}
}
for(int i=0; i<count1-1; i++) {
for(int j=i+1; j<count1; j++) {
if(!isGreaterThan(word[i],word[j])) {
String temp = word[j];
word[j] = word[i];
word[i] = temp;
}
}
}
for(int h=0; h<count2-1; h++) {
for(int k=h+1;k<count2; k++) {
if(num[h]<num[k]) {
Integer temp = num[h];
num[h] = num[k];
num[k] = temp;
}
}
}
array=null;
int g = count1 > count2 ? count1 : count2;
for(int l=0; l<g; l++) {
if(word[l]!=null) {
array[l] = word[l];
}
if(num[l]!=null) {
array[l] = Integer.toString(num[l]);
}
}
}
// 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;
}
}