Reputation: 192
I have a two data files, Text1_txt
and Text2_txt
. The structure and data within these files are as follows.
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
The left hand side column is an important column in this data file. Let's say this is an ID number. In my program I create a search based on this column.
Now the data of the 2nd file, 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
The main similarity between these two files is that they have same number of Columns and Rows. My program is to add the values of these two file. The addition depends upon the key attribute ID. I have an array in my program containing ID values. I compare these array values with the values in Text_1
. If the value of the ID column is the same as the ID value in the array, I then add up this row with corresponding row of Text_2
file. That means the 4th row of Text_1.txt
is add with 4th Row of Text_2.txt. This should happen in the code below. But this code adds the value of the 1st row of Text_2.txt
with all the Rows of Text1_txt
, but this is not what I want.
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);
}
}
}
}
}
Any suggestion about this matter is welcome. Thanks in advance
Upvotes: 0
Views: 73
Reputation: 272
maybe this is what u want...
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("=============================================================");
}
}
}
output result:
[12.0, 20.0, 22.0, 23.0, 25.0, 33.0, 45.0, 55.0, 77.0, 86.0, 90.0]
Text_1 matrix(before add) 86.000000 1.235600 -0.632500
Text_1 matrix(after add) 89.573232 -2.097239 1.708300
=========split line=========
Text_1 matrix(before add) 25.000000 1.051200 0.978520
Text_1 matrix(after add) 28.573225 -2.281637 3.319220
=========split line=========
Text_1 matrix(before add) 90.000000 3.568700 -0.897400
Text_1 matrix(after add) 93.573237 0.235775 1.443600
=========split line=========
Text_1 matrix(before add) 55.000000 -4.125400 1.361100
Text_1 matrix(after add) 58.573271 -7.458244 3.701800
=========split line=========
If modification based on your code, i would comment 19th line which is
while (x.hasNext())) {
and 32th line which is
}
then it will runs well too; the reason is: You should not make another inside loop here, it will read all of rows from Text_1.txt
after read first row of Text2.txt
,when the outside loop read second row or rest rows from Text_2.txt
, x.hasNext()
will always return false because it reached the end of Text_1.txt
already.
Upvotes: 1