简体   繁体   English

解析和排序后在行之间获取0.0

[英]Getting 0.0 inbetween lines after parse and sort

Read a file that has a line of random doubles. 读取一个具有随机双精度行的文件。 Read the file, sort the numbers, return the file. 读取文件,对数字进行排序,然后返回文件。 Did this all fine but when I print the sorted numbers I get a 0.0 in between each line. 一切都很好,但是当我打印排序的数字时,我在每行之间得到一个0.0。 I'm guessing I have an empty string at the end which is why I tried adding trim(), but no luck. 我猜我结尾处有一个空字符串,这就是为什么我尝试添加trim(),但没有运气的原因。 Any ideas? 有任何想法吗? Code and output below 下面的代码和输出

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);
  }
}

Here is what I get when the code is run. 这是代码运行时得到的信息。 All the numbers are sorted but I can't figure out why the 0.0 is there after each line. 所有的数字都进行了排序,但我不知道为什么每一行后都有0.0。 There is no final 0.0 as the System ends. 系统结束时没有最终的0.0。 Any ideas? 有任何想法吗?

-91.232 -90.65 -81.425 -80.503 -50.68 -45.588 -23.141 -0.665 18.004 29.005 92.292 93.923 -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 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 -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 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 -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 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 -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 0.0

-85.242 -83.407 -60.563 -47.319 -35.602 -22.979 -20.904 -16.537 25.004 55.298 69.193 -85.242 -83.407 -60.563 -47.319 -35.602 -22.979 -20.904 -16.537 25.004 55.298 69.193

0.0 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 -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 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 -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 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 -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 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 -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 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 -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 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 -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 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 -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 0.0

-96.917 -71.136 -45.42 -45.24 39.232 43.879 51.401 52.31 57.029 76.001 99.577 -96.917 -71.136 -45.42 -45.24 39.232 43.879 51.401 52.31 57.029 76.001 99.577

0.0 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 -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 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 -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 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 -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 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 -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 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 -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 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 -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 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 -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

If your input file has empty lines, it will print out "0.0". 如果您的输入文件有空行,它将打印出“ 0.0”。 Try checking if the read line has a length greater than 0. For example: 尝试检查读取的行的长度是否大于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");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM