please help me to figure out why the compiler tells me variable expected.
The error line is line 53, 54, and 63, 64.
I am very confused that I did this algorithms before in pervious tasks, and it passed perfectly. But in this task, I don't know why it keep telling me variable expected.
thank you.
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<Integer> indexNum = new ArrayList<Integer>();
ArrayList<Integer> numberList = new ArrayList<Integer>();
ArrayList<Integer> indexStr = new ArrayList<Integer>();
ArrayList<String> stringList = new ArrayList<String>();
ArrayList<String> resultList = new ArrayList<String>();
for (int i = 0; i < array.length; i++){
if (isNumber(array[i])) {
numberList.add(Integer.parseInt(array[i]));
indexNum.add(i);
} else {
stringList.add(array[i]);
indexStr.add(i);
}
}
// compare numbers
for (int i = numberList.size() - 1; i > 0; i--){
for (int j = 0; j < i; j++){
if (numberList.get(j) < numberList.get(j + 1)){
int buffer = numberList.get(j + 1);
numberList.get(j + 1) = numberList.get(j);
numberList.get(j) = buffer;
}
}
}
// compare strings
for (int i = numberList.size() - 1; i > 0; i--){
for (int j = 0; j < i; j++) {
if (!isGreaterThan(stringList.get(j), stringList.get(j + 1))) {
String buffer = stringList.get(j + 1);
stringList.get(j + 1) = stringList.get(j);
stringList.get(j) = buffer;
}
}
}
if (indexNum.get(0) > indexStr.get(0)) {
for (int i = 0; i < array.length; i++){
resultList.add(i, String.valueOf(numberList.get(i)));
resultList.add(i + 1, stringList.get(i));
}
} else {
for (int i = 0; i < array.length - 1; i++){
resultList.add(i + 1, String.valueOf(numberList.get(i)));
resultList.add(i, stringList.get(i));
}
}
array = resultList.toArray(new String[resultList.size()]);
}
// 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;
}
}