My program works, but the validator won't accept it. I've tried it with randomly capitalizing characters in words, having words with all caps, having words with no caps, it returns a completely alphabetical list but I'm not passing the hidden "mystery condition" - which I'm really getting sick of those. The validator should at least tell you when whatever program is entering the strings is doing stupid crap, and not just leaving you to guess that it's entering integers or whatever else it isn't supposed to be doing but is anyway.
Here's what my program prints out using the list of commented out words:
alphabetical
and
be
Display
Each
Enter
from
In
keyboard
line
new
on
order
Should
Task
the
them
twenty
word
words
And here's what it prints if I enter a bunch of nonsense. First, my nonsense as it was entered into the program:
zeeeee
zzzzz
fffff
eeeeeee
GGGGGGG
REEEGH
rHgUk
AiiIIiiII
Lee
LeeLee
Lee
Ill
rue
Etyo
ezzy
fri
EDDN
Weeeeee
wow
wOW
And the sorted list.
AiiIIiiII
EDDN
eeeeeee
Etyo
ezzy
fffff
fri
GGGGGGG
Ill
Lee
Lee
LeeLee
REEEGH
rHgUk
rue
Weeeeee
wow
wOW
zeeeee
zzzzz
it still works. I pass. Yay. Except I don't for some reason.
package com.codegym.task.task08.task0830;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
/*
Task about algorithms
*/
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
// String[] array = {"Task", "Enter", "twenty", "words", "from", "the", "keyboard", "and", "Display", "them",
// "In", "alphabetical", "order", "Each", "word", "Should", "be", "on", "new", "line"}; // this line
String[] array = new String[20];
for (int i = 0; i < array.length; i++) {
array[i] = reader.readLine();
}
sort(array);
for (String x : array) {
System.out.println(x);
}
}
public static void sort(String[] array) {
int lastUnsortedIndex;
for (lastUnsortedIndex = array.length -1; lastUnsortedIndex > 0; lastUnsortedIndex--) {
for (int i = 0; i < lastUnsortedIndex; i++) {
if (isGreaterThan(array[i], array[i + 1])) {
String temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
}
}
}
}
// String comparison method: 'a' is greater than 'b'
public static boolean isGreaterThan(String a, String b) {
return a.compareToIgnoreCase(b) > 0;
}
}