I am reading two files and I want for each line in file one sum up all the lines in file two. So far I have written the code below but it only does it for the first line in the files. Below is my code and sample files. Note I am getting java.util.NoSuchElementException
import java.io.*;
import java.util.Scanner;
import java.util.StringTokenizer;
public class knn {
public static void main (String[]args)throws IOException{
Scanner in = new Scanner(new FileInputStream("knn.txt"));
Scanner in2 = new Scanner(new FileInputStream("knn2.txt"));
while(in.hasNextLine()){
String linetoprocess = in.nextLine();
StringTokenizer st = new StringTokenizer(linetoprocess, " :");
while(in2.hasNextLine()){
String linetoprocess2 = in2.nextLine();
StringTokenizer st2 = new StringTokenizer(linetoprocess2, " :");
System.out.print("("+st.nextToken()+","+st2.nextToken()+"), ");
System.out.print("("+st.nextToken()+","+st2.nextToken()+"), ");
System.out.print("("+st.nextToken()+","+st2.nextToken()+"), ");
System.out.print("("+st.nextToken()+","+st2.nextToken()+"), ");
System.out.print("("+st.nextToken()+","+st2.nextToken()+"), ");
System.out.print("("+st.nextToken()+","+st2.nextToken()+"), ");
System.out.print("("+st.nextToken()+","+st2.nextToken()+"), ");
System.out.print("("+st.nextToken()+","+st2.nextToken()+"), ");
System.out.println("("+st.nextToken()+","+st2.nextToken()+")");
}
}
}
knn.txt
1 1:5.1 2:3.5 3:1.4 4:0.2
1 1:4.9 2:3.0 3:1.4 4:0.2
1 1:4.7 2:3.2 3:1.3 4:0.2
knn2.txt
1 1:5.4 2:3.7 3:1.5 4:0.2
1 1:4.8 2:3.4 3:1.6 4:0.2
1 1:4.8 2:3.0 3:1.4 4:0.1
1 1:4.3 2:3.0 3:1.1 4:0.1
I got it I should have called called the st.nextToken() before the inner while loop and store them in a variable which I can use later. Thanks for your suggestions. The complete code is below.
}