2013-10-28 95 views
-1

我正在創建一個程序,它將讀取一個txt文件(來自另一個主文本文件),取一串字符串@ -K),然後使用字符串中字符的ASCII值填充2d數組。所以我已經成功創建了2d數組並讀取了文件。問題是我的數組只是由字符串文件的第一個字母填充,而不是貫穿整個文件。我有一個if語句,我相信應該通過該文件讀取並正確填充它,但它沒有這樣做。無法弄清楚我做錯了什麼。這是代碼。從一個字符串填充一個二維數組,並將單個字符的ASCII值放入它

 public static void main(String[] args) throws IOException 
    { 
    String txtfile; 
    String txtfilecontents; 
    int matrix[][]= new int [24][34]; 
    int row=0; 
    int col=0; 
    BufferedReader masterfile = new BufferedReader(new FileReader("imageFileList.txt")); 
    while((txtfile=masterfile.readLine())!=null)            //reads master text file 
    { 
     BufferedReader imagefile= new BufferedReader(new FileReader(txtfile)); 
     while((txtfilecontents=imagefile.readLine())!=null)          //reads text file within master file 
     { 
     for(int i=0;i<txtfilecontents.length();i++)           
     { 
      if(col ==34) 
      { 
      col = 0; 
      row++; 
      } 
      if(row ==24) 
      { 
      col=0; 
      row=0; 
      break; 
      } 
      matrix[row][col] = (int)txtfilecontents.charAt(i)-64;         //instead of adding every character it only adds the first one until it fills up the 2d array 
      col++; 
     } 
     System.out.println(txtfilecontents); 
     for(row=0;row<24;row++) 
     { 
      for(col=0;col<34;col++) 
      System.out.printf("%4d",matrix[row][col]); 
      System.out.println(); 
     } 
     } 

     System.out.println(txtfile); 
    } 


    } 

下面是txtfile在其中的示例。

[email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@AGDFE[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@FH 

而且我已經加入了一堆文件中打印報表的,所以我可以看看我的輸出來臨了,但它會在以後取出來。

+0

給出的示例輸入,又該期望輸出是什麼?此外,減去64將不會給你任何東西的ASCII值... – nhgrif

+0

示例輸出應該是用輸入的ascii值填充的2d數組。減去64只是爲了讓我得到0-11的值(@ ASCII值是64,減去64 = 0等) – Student

+0

所以你的輸出應該是ASCII值還是ASCII值減去64 ...? – nhgrif

回答

0
for(int i=0;i<txtfilecontents.length();i++)           
     { 
      for(row=0;row<24;row++) 
      for(col=0;col<34;col++) 
       matrix[row][col] = (int)txtfilecontents.charAt(i);        //instead of adding every character it only adds the first one until it fills up the 2d array 
     } 

你用的charAt(I)填充矩陣,所以在最後,將基字符串中充滿了最後一個字符

嘗試是這樣的:

row = 0; 
col = 0; 
for(int i=0;i<txtfilecontents.length();i++)           
{ 
    //Insert the char into the matrix 
    matrix[row][col] = (int)txtfilecontents.charAt(i); 

    //Move the matrix position for the next character 
    row++; 
    if (row >= 24){ 
     row = 0; 
     col++; 
     if (col >= 34){ 
      System.out.println("Matrix out of space"); 
     } 
    } 
} 
+0

我的打印聲明顯示它正在做什麼。我嘗試了@Floris的建議,但它只填充了第一個數字,然後給了我一堆0。 – Student

+0

@Student爲我的回答添加了一個可能的解決方案 – Evans

+0

謝謝我使用了一些非常相似的方法來修復它,但是現在我收到一個錯誤,它不會讀取下一個txt文件。我在主要問題中更新了我的代碼。 – Student

0

循環

for(int i=0;i<txtfilecontents.length();i++)           
     { 
      for(row=0;row<24;row++) 
      for(col=0;col<34;col++) 
       matrix[row][col] = (int)txtfilecontents.charAt(i);        //instead of adding every character it only adds the first one until it fills up the 2d array 
     } 

保存在每一個位置上的字符。你需要計算你想要保存的地址,而不是圍繞內部for循環。或者,也許更新最內層循環中的計數器i,並設置一個標誌以在達到字符串末尾時突破。第一種方法的

實施例(編輯 - 未遞增COL

row=0;col=0; 
for(int i=0;i<txtfilecontents.length();i++)           
    { 
     if (col == 34) { 
      col=0; row++; 
     } 
    if (row==24) break; 
    matrix[row][col] = (int)txtfilecontents.charAt(i);   
    col++; // <<<<< added this line!!      
    } 
+0

我在我的代碼中省略了一行......不會遞增「col」。它現在應該工作。 – Floris

相關問題