使用扫描仪从两个文件中添加值不起作用

时间:2018-06-19 12:57:06

标签: java arrays file while-loop java.util.scanner

我有两个数据文件,Text1_txtText2_txt。这些文件中的结构和数据如下。

Text_1.txt:

86.0    1.2356    -0.6325

25.0    1.0512  0.97852

30.0    2.6581  2.25147

18.0    -0.2358 1.5689

90.0    3.5687  -0.8974

88.0    1.9852  -0.1478

70.0    2.0789  -1.2564

87.0    9.2547  1.1120

55.0    -4.1254 1.3611

左侧列是此数据文件中的重要列。假设这是一个ID号。在我的程序中,我基于此列创建搜索。

现在第二个文件Text_2.txt的数据:

3.57323240   -3.33283870     2.34080000 

3.57322470   -3.33283680     2.34070000 

3.57323800   -3.33286720     2.34080000 

3.57324670   -3.33288070     2.34080000 

3.57323740   -3.33292520     2.34100000 

3.57322660   -3.33292540     2.34110000 

3.57318870   -3.33289200     2.34110000 

3.57319510   -3.33287860     2.34110000 

3.57327090   -3.33284380     2.34070000 

这两个文件之间的主要相似之处在于它们的列数和行数相同。我的程序是添加这两个文件的值。附加内容取决于密钥属性ID。我的程序中有一个包含ID值的数组。我将这些数组值与Text_1中的值进行比较。如果ID列的值与数组中的ID值相同,则将这一行与Text_2文件的相应行相加。这意味着Text_1.txt的第四行与Text_2.txt的第四行相加。这应该在下面的代码中发生。但是此代码将Text_2.txt的第一行的值与Text1_txt的所有行相加,但这不是我想要的。

import Jama.Matrix;
import java.io.File;
import java.util.Arrays;
import java.util.Scanner;


public class T5 {
public static void main(String args[])throws Exception{
    double arr[]= new double[]{86.0,12.0,55.0,90.0,77.0,22.0,25.0,33.0,45.0,20.0,23.0};// The array with which the value of file Text_1 compare
    Arrays.sort(arr);
    System.out.println(Arrays.toString(arr));
    Scanner x= new Scanner(new File("D:\\Text_1.txt"));
    Scanner y=new Scanner(new File("D:\\Text_2.txt"));
    while(y.hasNext()) {
        double x1=y.nextDouble();
        double y1=y.nextDouble();
        double z1=y.nextDouble();
        Matrix ini_x=new Matrix(new double[][]{{x1},{y1},{z1}});
        while (x.hasNext()) {
            double d = x.nextDouble();
            double e = x.nextDouble();
            double f = x.nextDouble();
            double index = Arrays.binarySearch(arr, d);
            if (index >= 0 && index <= arr.length) {
               System.out.println("Element within the array" + d);//output those number which are both in array and file
                Matrix A = new Matrix(new double[][]{{d}, {e}, {f}});
                ini_x.print(9,6);
                Matrix c=ini_x.plus(A);// Matrix addition (add the first row of Text_2.txt with all the four rows which found using binarySearch.
                c.print(9,6);
            }

        }
    }

}
}

欢迎对此事提出任何建议。 预先感谢

1 个答案:

答案 0 :(得分:1)

也许这就是你想要的...

public static void main(String[] args)
{
    double arr[] = new double[]{
            86.0,
            12.0,
            55.0,
            90.0,
            77.0,
            22.0,
            25.0,
            33.0,
            45.0,
            20.0,
            23.0
    };// The array with which the value of file Text_1 compare
    Arrays.sort(arr);
    System.out.println(Arrays.toString(arr));
    Scanner t1 = new Scanner(TestJama.class.getResourceAsStream("/Text_1.txt"));
    Scanner t2 = new Scanner(TestJama.class.getResourceAsStream("/Text_2.txt"));
    while (t1.hasNext())
    {
        double t1_id    = t1.nextDouble();
        double t1_col2    = t1.nextDouble();
        double t1_col3    = t1.nextDouble();
        Matrix t1_matrix = new Matrix(new double[][]{
                {t1_id},
                {t1_col2},
                {t1_col3}
        });

        //read the same row from Text_2.txt
        double t2_col1 = t2.nextDouble();
        double t2_col2 = t2.nextDouble();
        double t2_col3 = t2.nextDouble();

        double index = Arrays.binarySearch(
                arr,
                t1_id
        );
        if (index>0){
            Matrix t2_matrix = new Matrix(new double[][]{
                    {t2_col1},
                    {t2_col2},
                    {t2_col3}
            });
            System.out.print("Text_1 matrix(before add)");
            t1_matrix.print(9,6);
            System.out.print("Text_1 matrix(after add)");
            t1_matrix.plus(t2_matrix).print(9,6);
            System.out.println("=============================================================");
        }

    }
}

输出结果:

[12.0,20.0,22.0,23.0,25.0,33.0,45.0,55.0,77.0,86.0,90.0]

Text_1矩阵(添加之前)   86.000000    1.235600   -0.632500

Text_1矩阵(添加后)   89.573232   -2.097239    1.708300

==========分割线=========

Text_1矩阵(添加之前)   25.000000    1.051200    0.978520

Text_1矩阵(添加后)   28.573225   -2.281637    3.319220

==========分割线=========

Text_1矩阵(添加之前)   90.000000    3.568700   -0.897400

Text_1矩阵(添加后)   93.573237    0.235775    1.443600

==========分割线=========

Text_1矩阵(添加之前)   55.000000   -4.125400    1.361100

Text_1矩阵(添加后)   58.573271   -7.458244    3.701800

==========分割线=========

如果根据您的代码进行修改,我会在第19行添加注释

while (x.hasNext())) {

第32行是

}

然后它将运行良好;原因是:您不应在此处进行另一个内部循环,当外部循环从{{1}开始读取第二行或其余行时,它将在读取Text_1.txt的第一行之后从Text2.txt读取所有行。 }},Text_2.txt将始终返回false,因为它已经到达x.hasNext()的末尾。