Reputation: 43
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new FileReader(new File(args[0])));
String line;
while ((line = in.readLine()) != null) {
StringTokenizer st = new StringTokenizer(line);
int len = st.countTokens();
Double[] seq = new Double[len];
for (int i = 0; i < len; i++)
seq[i] = Double.parseDouble(st.nextToken());
Arrays.sort(seq);
for (int i = 0; i < len; i++) {
if (i > 0) System.out.print(" ");
System.out.print(seq[i]);
} System.out.print("\n");
}
}
}
So I'm trying to solve this CodeEval problem (https://www.codeeval.com/open_challenges/91/) and my solution is not getting through all the test cases. I think my method of output is correct (spaces between numbers, trailing newline). I can't figure out what may be going on in the sorting or anywhere else.
The solution is apparently not correct when using floats either.
Upvotes: 1
Views: 568
Reputation: 19203
You may be sorting correctly but printing incorrectly. Decimal numbers are represented approximately. The runtime attempts to show them in the short format, but that is not guaranteed.
All the examples they gave should work, but who knows what the test suite does in the background.
Upvotes: 2
Reputation: 129497
I also think this is a printing problem. The output seems to require exactly 3 decimal places on each number, based on the sample I/O. But, if you print out a double like 70.920
(one of the example inputs) it will display as 70.92
.
double d = 70.920;
System.out.println(d);
System.out.printf("%.3f", d); // <-- try this
70.92 70.920
Notice how the second output is consistent with the format of the sample output whereas the first is not.
Upvotes: 2
Reputation: 533472
I would use double
not Double
and I would only sort the values after reading the all, not after every line.
Perhaps some inputs have more than one line?
Upvotes: 1