2015-10-28 98 views
2

我想寫包含這樣雙打的列表中的程序,讀取文本文件(罰款與此):二維陣列卡住

1.0, 2.0, 3.0 
4.0, 5.0, 6.0 
7.0, 8.0, 9.0 

_

public static void main(String[] args) { 

    Scanner inputStream = null; 

    try { 
     inputStream = new Scanner(new FileInputStream("tstc.txt")); 
    } 
    catch (FileNotFoundException e) { 
     System.out.println(e.getMessage()); 
     System.out.println("Program aborted."); 
     System.exit(0); 
    } 

    double[][] arrayNums = new double[3][3]; 

    for(int sec = 0; sec < 3; sec++) { 
     while(inputStream.hasNextLine()) { 
      String line = inputStream.nextLine(); 
      String[] lineList = line.split(", "); 
      int pos = 0; 

      for(int motor = 0; motor < 3; motor++) { 
       double lineDouble = Double.parseDouble(lineList[pos]); 
       arrayNums[motor][sec] = lineDouble; 
       pos+=1; 
      } 
     } 
    } 
    System.out.println(Arrays.deepToString(arrayNums)); 

這就是我去爲:

[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]] 

,但是這是我得到:

[[7.0, 0.0, 0.0], [8.0, 0.0, 0.0], [9.0, 0.0, 0.0]] 

我已經看到我的問題在於變量保持不變,但我不確定如何在不更改整個代碼結構的情況下重新排列代碼。

任何提示將不勝感激。

+1

你不需要秒循環因爲你已經有了InputStream一個 –

回答

2

這應該像預期的那樣工作。

int pos = 0; 
    while(inputStream.hasNextLine()) { 
     String line = inputStream.nextLine(); 
     String[] lineList = line.split(", "); 

     for(int motor = 0; motor < 3; motor++) { 
      double lineDouble = Double.parseDouble(lineList[motor]); 
      arrayNums[pos][motor] = lineDouble; 

     } 
     pos++; 
    } 
System.out.println(Arrays.deepToString(arrayNums)); 

不要忘記刪除第一個循環。

+1

首先,非常感謝你的幫助!問題是,實際上,我將閱讀的文本文件將包含數百個列(要讀取的行)。這就是爲什麼我真的想要構造arrayNums像這樣:arrayNums [motor] [pos]。爲了節省內存以及更實用和易於瀏覽。 [電機]實際上應該表示一輛汽車,而[pos]表示速度。文件中的每一行都是隨後的第二行,說明汽車的速度。 無論如何要適應這個?對不起,以前不清楚。 –

+0

@AndréFoote如果我理解得很好,你應該改變你的變量名稱。但是如果你有另一個問題,不要猶豫,創建一個新的,因爲這個已經得到了回答,它也會得到其他用戶的更多關注。 –

1
int sec = 0; 
while (inputStream.hasNextLine()) { 
    String line = inputStream.nextLine(); 
    String[] lineList = line.split(", "); 

    for (int i = 0; i < lineList.length; i++) { 
     arrayNums[sec][i] = Double.parseDouble(lineList[i]); 
    } 

    sec++; 
} 

這就是說,如果你沒有預料到在你的文件中的行數(即有更多的文件中的行不是有你的陣列中的行),異常會在內部for循環拋出

+0

首先,非常感謝您的幫助!問題是,實際上,我將閱讀的文本文件將包含數百個列(要讀取的行)。這就是爲什麼我真的想要構造arrayNums像這樣:arrayNums [i] [sec]。爲了節省內存以及更實用和易於瀏覽。 [i]實際上應該表示一輛汽車,而[秒]表示速度。文件中的每一行都是隨後的第二行,說明汽車的速度。 無論如何要適應這個?對不起,以前不清楚。 –

1

您需要刪除while循環。

這將使你的預期結果:[1.0,2.0,3.0],[4.0,5.0,6.0,7.0,8.0,9.0]

import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.util.Arrays; 
import java.util.Scanner; 

public class ReadMatrix { 

    public static void main(String[] args) { 

     String fileName = "tstc.txt"; 
     int nRow = 3; 
     int nCol = 3; 
     double[][] arrayNums = new double[nRow][nCol]; 

     try (Scanner inputStream = new Scanner(new FileInputStream(fileName))) { 

      for (int i = 0; i < nRow; i++) { 
       String line = inputStream.nextLine(); 
       String[] lineList = line.split(", "); 

       for (int j = 0; j < nCol; j++) { 
        double lineDouble = Double.parseDouble(lineList[j]); 
        arrayNums[i][j] = lineDouble; 
       } 
      } 
     } catch (FileNotFoundException e) { 
      System.out.println(e.getMessage()); 
      System.out.println("Program aborted."); 
      return; 
     } 
     System.out.println(Arrays.deepToString(arrayNums)); 
    } 
}