从两个文件中读取并采用跨产品

时间:2012-04-03 17:55:15

标签: java stringtokenizer

我正在阅读两个文件,我想要文件中的每一行,总结文件二中的所有行。到目前为止,我已经编写了下面的代码,但它只针对文件的第一行。下面是我的代码和示例文件。注意我收到了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

2 个答案:

答案 0 :(得分:0)

StringTokenizer已经过时了,支持String.split(...)

但就修复你的代码而言,我建议你应该在StringTokenizer.hasMoreTokens周围添加另一个

答案 1 :(得分:0)

我知道我应该在内部while循环之前调用st.nextToken()并将它们存储在一个我以后可以使用的变量中。谢谢你的建议。完整的代码如下。

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

    while(in.hasNextLine()){
        String linetoprocess = in.nextLine();
        StringTokenizer st = new StringTokenizer(linetoprocess, " :");
        Scanner in2 = new Scanner(new FileInputStream("knn2.txt"));
        String a = st.nextToken();
        String b = st.nextToken();
        String c = st.nextToken();
        String d = st.nextToken();
        String e = st.nextToken();
        String f = st.nextToken();
        String g = st.nextToken();
        String h = st.nextToken();
        String i = st.nextToken();

        while(in2.hasNextLine()){
        String linetoprocess2 = in2.nextLine();
        StringTokenizer st2 = new StringTokenizer(linetoprocess2, " :");
        System.out.print("("+a+","+st2.nextToken()+"), ");
        System.out.print("("+b+","+st2.nextToken()+"), ");
        System.out.print("("+c+","+st2.nextToken()+"), ");
        System.out.print("("+d+","+st2.nextToken()+"), ");
        System.out.print("("+e+","+st2.nextToken()+"), ");
        System.out.print("("+f+","+st2.nextToken()+"), ");
        System.out.print("("+g+","+st2.nextToken()+"), ");
        System.out.print("("+h+","+st2.nextToken()+"), ");
        System.out.println("("+i+","+st2.nextToken()+")");

        }
    }
}

}