读取具有一行随机双精度的文件。读取文件,对数字进行排序,返回文件。 这一切都很好,但是当我打印排序的数字时,我在每行之间得到0.0。我猜我最后有一个空字符串,这就是为什么我尝试添加trim(),但没有运气。有任何想法吗?
下面的代码和输出import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
public class sortExample {
public static void main (String[] args) throws IOException {
String file = "C:\\Users\\xxxx\\Desktop\\new.txt";
Scanner sc = new Scanner(new FileReader(file));
String line;
while (sc.hasNext()) {
line = sc.nextLine();
line.trim();
String[] lineArray = line.split("\\s");
double[] nums = new double[lineArray.length];
if (lineArray.length > 0) {
for (int i = 0; i < lineArray.length; i++) {
if (!lineArray[i].isEmpty()) {
nums[i] = Double.parseDouble(lineArray[i]);
}}
Arrays.sort(nums);
}
for (int i = 0; i < nums.length-1; i++) {
System.out.print(nums[i] + " ");
}
System.out.print(nums[nums.length-1]);
System.out.print("\n");
}
System.exit(0);
}
}
以下是代码运行时的结果。所有数字都已排序,但我无法弄清楚为什么每条线后面都有0.0。系统结束时没有最终的0.0。有什么想法吗?
-91.232 -90.65 -81.425 -80.503 -50.68 -45.588 -23.141 -0.665 18.004 29.005 92.292 93.923
0.0
-100.835 -99.504 -80.183 -72.063 -71.447 -63.888 -47.389 -45.882 -37.815 -37.56 -22.952 -20.448 23.598 48.676 55.724 65.639 67.449 70.038
0.0
-78.977 -78.528 -72.272 -70.805 -64.709 -44.632 -42.855 -23.822 -22.273 -10.833 -1.157 7.712 21.619 21.935 23.442 37.869 42.056 78.46 94.735
0.0
-92.446 -84.111 -47.699 -23.366 -8.725 -1.679 7.685 23.537 32.703 67.569 68.633 72.266
0.0
-85.242 -83.407 -60.563 -47.319 -35.602 -22.979 -20.904 -16.537 25.004 55.298 69.193
0.0
-70.442 -39.916 -25.097 -8.729 -1.194 -0.043 7.086 11.874 19.538 35.647 44.886 52.162
0.0
-98.469 -80.931 -73.274 -55.879 -54.946 -54.695 -52.389 -45.66 -29.34 -12.44 -12.171 16.25 16.536 45.065 97.759
0.0
-65.594 -50.741 -49.607 -36.255 -27.512 -1.492 1.905 10.135 40.764 63.527 66.459 79.457 95.891
0.0
-75.088 -71.983 -64.298 -52.566 -33.779 -26.999 -19.76 -12.022 30.107 32.164 44.109 69.123 71.333
0.0
-100.822 -91.321 -58.742 -51.631 -6.001 -1.338 5.147 13.478 14.336 63.754 66.76 69.227
0.0
-100.729 -87.041 -51.238 -30.391 -19.053 -12.027 -1.812 9.104 38.951 41.738 45.416 57.447 80.157 94.37
0.0
-100.733 -96.084 -66.776 -64.397 -48.363 -38.223 11.665 13.101 22.904 30.637 40.223 61.489 67.105 86.601 98.225
0.0
-96.917 -71.136 -45.42 -45.24 39.232 43.879 51.401 52.31 57.029 76.001 99.577
0.0
-95.874 -91.529 -61.868 -56.623 -56.55 -43.048 -37.933 -33.65 -32.251 -31.507 -14.625 -1.828 34.268 59.821 60.48 73.106 75.763 89.408 89.551
0.0
-90.637 -77.109 -71.369 -64.957 -60.957 -52.252 -45.577 -34.413 -23.08 -22.805 27.066 34.148 39.28 81.409 90.394 91.746
0.0
-100.389 -99.758 -61.022 -26.942 -18.452 -14.1 -6.847 18.504 21.213 47.721 67.033 72.152
0.0
-86.559 -85.971 -80.617 -43.239 -41.397 -30.985 -22.344 -5.222 -3.042 3.629 7.885 24.202 33.706 58.209 67.877 92.776 96.691 98.549
0.0
-90.386 -79.406 -72.129 -56.667 -55.158 -54.217 -37.413 -28.465 8.949 14.774 24.166 24.632 34.977 35.126 59.208 80.778 84.792
0.0
-80.957 -60.479 -55.715 -30.557 -24.367 -10.497 -1.073 30.088 66.313 74.442 86.75 89.186
0.0
-88.962 -65.577 -44.427 -32.155 -32.106 -26.038 -22.205 -21.784 -14.312 -12.412 -5.275 10.442 12.684 33.622 33.838 41.632 50.094 67.565 75.008 89.463
答案 0 :(得分:4)
如果您的输入文件有空行,则会打印出&#34; 0.0&#34;。尝试检查读取行的长度是否大于0.例如:
if (line.length() > 0) {
String[] lineArray = line.split("\\s");
double[] nums = new double[lineArray.length];
if (lineArray.length > 0) {
for (int i = 0; i < lineArray.length; i++) {
if (!lineArray[i].isEmpty()) {
nums[i] = Double.parseDouble(lineArray[i]);
}
}
Arrays.sort(nums);
}
for (int i = 0; i < nums.length - 1; i++) {
System.out.print(nums[i] + " ");
}
System.out.print(nums[nums.length - 1]);
System.out.print("\n");
}