2016-03-24 16 views
-1

私はこのようになりますテキストファイルを持っている:Javaの2つの列だけでテキストファイルとフォーマットを読み込む方法は?

House1 25456 22456 54564 54564 
House2 54788 54756 

ハウス(それはアップトン1500軒の家屋することができる)

を、私はそれが次のようになりたい:

House1 25456 
House1 22456 
House1 54564 
House1 54564 
House2 54788 
House2 54756 

House(again same principle) 

コード

public class Houses { 

    public static void main(String[] args) { 
     UsingScanner(); 
    } 
    public static void UsingScanner(){ 
     try{ 
      Scanner sc = new Scanner (new File("C:\\Test.txt")); 
      while (sc.hasNextLine()){ 
       System.out.println(sc.nextLine()); 
      } 
     }catch(Exception e){ 
      System.out.println("Ups! you got a problem"); 
     } 
    } 
} 
+0

メソッド名にキャメルケースを使用してください。 – bcsb1001

答えて

2

String.split()を使用してハウスキーを抽出します。その後、それらをループして印刷します(または必要に応じてStringBuilderに収集します)。例えば:outはOutputStreamのであると仮定すると、

  while (sc.hasNextLine()){ 
       String[] tokens = sc.nextLine().split("\\s+"); 
       String houseName = tokens[0]; 
       for(int i=1; i< tokens.length ; i++) 
        System.out.println(houseName + " " + tokens[i]); 
      } 
+0

私はあなたのコードを次のように使用しました。しかし私は、House(Number)がそれのそばにコードを持っていないかもしれないという条件があることに気付いた。例えば、house2は、任意のコードを持っていない: House1 25456 House2 House3 87888 88889 87724 私はそれらをプリントアウトする前であれば条件を追加する必要がありますと思います。 – joshua

1
String[] parts = sc.nextLine().split(); 
for (int i=1;i<parts.length;i++){ 
    out.write(parts[0]+" "+parts[i]); 
} 

を。

+0

コメントありがとうございました! – joshua

0

私は可能な限り単純で、あなたの元のコードに関してはあまり変化の可能性を持つようにそれを維持しようとした:

import java.io.File; 
import java.io.FileWriter; 
import java.util.Scanner; 

public class Houses { 

    public static void main(String[] args) { 
     UsingScanner(); 
    } 

    public static void UsingScanner() { 
     try { 
      Scanner sc = new Scanner(new File("C:\\Test.txt")); 
      //fw is to write you desired result into the Output.txt file 
      FileWriter fw = new FileWriter(new File("C:\\Output.txt")); 
      while (sc.hasNextLine()) { 
       //split your line by spaces into an array of Strings 
       String[] splitted = sc.nextLine().split(" "); 
       //at index zero of the array there is always the house name 
       for (int k = 1; k < splitted.length; k++) { //so start the loop from index 1 
        //loop every numeric item of a line, write the house name and the numeric item 
        fw.write(splitted[0] + " " + splitted[k] + System.getProperty("line.separator")); 
       } 
       //step to the next house name (another line) 
      } 
      fw.close(); 
     } catch (Exception e) { 
      System.out.println("Ups! you got a problem"); 
     } 
    } 
} 
+0

共有する以上!最終目標は、提案したように新しい文書に保存することです。私はすでに出力をコード化する必要があります。簡単にするために、putだけがsystem.out.println()にあります: – joshua

+0

上記のFileライターを試しましたが、分割された[0]スペース分割された[k]にデータを保存せずに新しい行を返します。それは何をしているのか、すべてを1行に印刷しています。 – joshua

+0

@ joshuaおそらくあなたがWindowsを使用していて、文字 '\ n'(これは以前の回答バージョンのすべてのループの最後に追加されています)は、実際に新しい行を表示するには不十分です。 これで、 '\ n' whit System.getProperty( "line.separator")(これはプラットフォームに依存する行区切り文字が返されます)を置き換えて答えを修正しました。 – WoDoSc

関連する問題