2012-04-03 2 views
0

Je lis deux fichiers et je veux pour chaque ligne dans le fichier un résumer toutes les lignes dans le fichier deux. Jusqu'à présent, j'ai écrit le code ci-dessous, mais il ne le fait que pour la première ligne dans les fichiers. Voici mon code et mes fichiers d'exemple. Attention, je veux dire java.util.NoSuchElementExceptionLire à partir de deux fichiers et prendre des produits croisés

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

+0

fournir une sortie de l'échantillon afin que nous puissions voir ce que vous essayez de faire réellement. –

+0

(1,1), (1,1), (5,1,5,4), (2,2), (3,5,3,7), (3,3), (1,4,1,5), (4,4), (0,2,0,2) ----- (1,1), (1,1), (5,1,4,8), (2,2), (3,5,3,4), (3,3), (1,4,1,6), (4,4), (0,2,0.2) – fanbondi

+0

Je l'ai eu j'aurais dû appeler appelé st.nextToken() avant la boucle while interne. Merci pour vos suggestions. – fanbondi

Répondre

0

Je l'ai eu j'aurais dû appeler appelé st.nextToken() avant la boucle while interne d les stocker dans une variable que je pourrai utiliser plus tard. Merci pour vos suggestions. Le code complet est ci-dessous.

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

     } 
    } 
} 

}

0

StringTokenizer est sorti de la mode, en faveur de String.split (...)

Mais dans la mesure où la fixation de votre code, je vous suggère devrait en ajouter un autre tout autour StringTokenizer.hasMoreTokens

+0

Je l'ai eu j'aurais dû appeler appelé le st.nextToken() avant la boucle while interne. Merci pour vos suggestions. – fanbondi

Questions connexes