2016-04-04 21 views
-1

通常のテキストファイルをダブル配列に読み込みたいと思います。私は、それ自体(基本的に10で割った場合の小数に第9の属性を持っていると思い配列にこれを読んでながらしかしテキストファイルを配列に読み込んで変更する

5,1,1,1,2,1,3,1,1,2 

5,4,4,5,7,10,3,2,1,4 

3,1,1,1,2,2,3,1,1,2 

6,8,8,1,3,4,3,7,1,4 

4,1,1,3,2,1,3,1,1,2 

:テキストファイル(線間にスペースなし除く)次の形式であります)と最後の数(2または4)は、2D配列の10番目の列に0を入力する場合は2番目の列を、11番目の場合は0を入力する場合は「列」を分離する必要があります。 本質的には次のようにフォーマットされています。

{0.5, 0.1, 0.1, 0.1, 0.2, 0.1, 0.3, 0.1, 0.1, 0, 1}, 

{0.5, 0.4, 0.4, 0.5, 0.7, 1, 0.3, 0.2, 0.1, 1, 0}, 

ご協力いただければ幸いです。

for (String str : values) { 
    double str_double = Double.parseDouble(str); 
    myDouble[x][y]=str_double/10;        
    System.out.print(myDouble[x][y] + " "); 
    y = y + 1; 
} 
+1

こんにちは。何を試しましたか?あなたが得ているどんなエラー? SO( – Prashant

+0

)の質問方法については、(this)[stackoverflow.com/help/how-to-ask]を参照してください。現在、ファイルをダブル配列にしてから、すべてを10で割って修正しています。 (文字列str:値)のための 」 \t \t { \t \t \tダブルstr_double = Double.parseDouble(STR)。 \t \t \t myDouble [x] [y] = str_double/10; \t \t \t System.out.print(myDouble [x] [y] + ""); \t \t \t \t \t y = y + 1; \t \t} ' 各行の最後の数値を指定し、それを2つの異なる「列」に変換する方法がわかりません –

+0

サイズが固定されていない場合はlength()関数を使用して計算します。 – Alok

答えて

0

良い答えは何が行われたかを説明するので、私はファイルIOを含む最も重要な部分についてコメントしました。

result.lengthresult[row].lengthの違いに注意してください。

1つは配列の1次元の長さを示し、2番目の配列は2次元配列の特定の位置に含まれる配列の次元を示します。

Javaでは、内部配列のサイズが異なるため、多次元配列は「ギザギザ」配列として定義されます。

この回答によって必要なものが明確になっているかどうかを確認し、さらに質問があるかどうかを確認してください。

編集:質問からの仕様を満たす修正されたコード。希望の出力を確認するために実行してください。

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileReader; 
import java.io.IOException; 

public class Example { 

    private static final String DELIMITER = ","; 
    private static final int LINE_COUNT = 5; 
    private static final int COL_COUNT = 11; 

    public static void main (String[] argv) { 

     //Create a reference to the file 
     File incoming = new File("C:\\DEV\\input_file.txt"); 

     try { 

      //Create the components to read the file 
      FileReader fr = new FileReader(incoming); 
      BufferedReader br = new BufferedReader(fr); 
      String line = ""; 
      String[] splitLine; 

      /*Can have your program read the file to count lines and assign proper size instead of hardcode*/ 
      double[][] result = new double[LINE_COUNT][COL_COUNT]; 

      //Count the line being processed 
      int row = 0; 

      //This reads each line of the file 
      while ((line = br.readLine()) != null) { 

       //Splits the line around delimiter (,) 
       splitLine = line.split(DELIMITER); 

       //Iterates across the array of elements split from the string line 
       for (int col = 0; col < splitLine.length; col++) { 

        //In case the element is the last 
        if (col == splitLine.length - 1) { 

         //Select the desired behavior 
         if (splitLine[col].equals("2")) { 
          result[row][col] = 0; 
          result[row][col + 1] = 1; 
         } else if (splitLine[col].equals("4")) { 
          result[row][col] = 1; 
          result[row][col + 1] = 0; 
         } 

        } else { 
         //Otherwise, assign the value divided by 10 
         result[row][col] = Double.parseDouble(splitLine[col])/10; 
        } 
       } 

       //Increases the rowcount 
       row++; 
      } 

      //Closes the resource - You can research about "try with resources for automatic closing" 
      br.close(); 
      fr.close(); 

      //Test 
      for (int i = 0; i < result.length; i++) { 
       System.out.print("{"); 
       for (int j = 0; j < result[i].length; j++) { 
        System.out.print(result[i][j]); 
        if (j != result[i].length - 1) { 
         System.out.print(","); 
        } 
       } 
       System.out.print("}"); 
       if (i != result.length - 1) { 
        System.out.print(","); 
       } 
      } 

     } catch (IOException e) { 
      //Print error if file not found 
      System.out.println("Can't read the file"); 
     } 
    } 
} 
+1

メモリリークの問題を避けるために 'FileReader'と' BufferedReader'のインスタンスを自動的に閉じることができるように、 'try-with-resource'を使うことができます。 – Prashant

+0

これはある程度は機能します。私が見つけた唯一の問題は、最後の行の複数の繰り返しを印刷するということです。 –

+0

@Charles Braybrook - コードを変更しました。どうぞご覧ください。乾杯。 – rafaelbattesti

関連する問題