2016-12-03 3 views
-1

300行と785列の.txtファイルがあり、E表記の数字の束(例:-6.431571950262252035e-02)です。これらの数値を2次元配列に変換するにはどうしたらいいですか? (なお、各行の文字列の配列を取得するためにString::split()を使用し、その後、次の行をフェッチするために多くの行がある一方でループへJAVA:txtファイルをE記法で数値を持つ2次元配列に変換するにはどうすればよいですか?

double[][] hiddenArray = new double[300][785]; 

Scanner scanner = new Scanner(System.in) ; 
String hiddenFile = "hidden-weights.txt"; 
String outputFile = "output-weights.txt"; 
scanner.close(); 

Scanner in = new Scanner(new File(hiddenFile)); 
String hidden= in.nextLine(); 

答えて

0

使用Scanner::hasNext()Scanner::nextLine()::私の

これは私が持っているすべてであります列がコンマで区切られている(つまり、,)が、あなたのニーズに合わせて自由に調整できることが前提です。数字を解析するにはDouble.valueof()を使用し、その値を配列に追加してください(例:hiddenArray)。

以下のサンプルを使用することができます。私もtutorialspoint.com codingGroundの例を作成しましたが、うまくいかないかもしれません...

try { 
     String delimiter = ","; //separates columns - change for your needs 

     int row = 0; 
     Scanner in = new Scanner(new File(hiddenFile)); 
     String line; 
     while (in.hasNext() && (line = in.nextLine()) != null) { 
      String[] vals = line.trim().split(","); 

      for (int col = 0; col < vals.length; col++) { 
       hiddenArray[row][col] = Double.valueOf(vals[col]); 
      } 
      row++; 
     }  
    } 
    catch(FileNotFoundException e) { 
     System.out.println("file not found - "+e.getMessage()); 
    } 
関連する問題