2012-10-27 59 views
1

我需要用2D數組中的位置[0] [0]處的字符串值標記迷宮的開始。問題是每次讀取新行時,位置[i] [0]處的值都會改變,我需要它始終是位置[0] [0]處的第一個值。沒有空格或分隔符,因此我正在讀取每行中每行的每個char值。從txt.file讀入二維數組的數據值正在翻轉

我不知道如何得到它只是看到在迷宮[0] [0]字符串是我想設置爲1的字符串?

迷宮的佈局使第一行有2個整數; 1st int =迷宮中的行數,2nd int =迷宮中的列數。開始始終爲[0] [0],結束始終爲[第1行] [col-1]。其餘的行是迷宮。因此,在文本文件中的第2行=迷宮的第0行。

因此,我將該文件作爲字符串值讀取,然後將第一行字符串值解析爲int,並使用這些值來定義我的二維整數數組。

然後我通讀剩餘的行,並將它們與第一個char值進行比較,該值始終等於構成路徑的值(如果存在)。如果讀取的符號= =,那麼它被存儲爲1,否則它被存儲爲0.然後我要通過二維數組並開始比較不同的座標以確定路徑並將該信息存儲在一個隊列,然後一個堆棧。

我遇到的問題是,它對前3行起作用,但其餘行的值會翻轉。我附加了代碼片段,它正在讀取的.txt文件。我知道這是因爲它將自己與每行的第一個元素進行比較,對於前3行,它是「。」然後爲其餘行添加「+」。

import java.io.*; 
import java.util.*; 

public class MazeArray { 
    String info; 
    String[] rowCol; 
    //private char [][] maze; 
    private int [][] maze; 
    private Scanner readFile; 
    private int path, cur, row, col; 

public MazeArray() 


{ 
    maze = new int[0][0]; 
} 

public int[][] readFile(String fileName) throws IOException 
{ 
    readFile = new Scanner(new File(fileName)); 

    info = readFile.nextLine(); 
    rowCol = info.split(" ",2); 
    row = Integer.parseInt(rowCol[0]); 
    col = Integer.parseInt(rowCol[1]); 
    maze = new int[row][col]; 
    System.out.println("# of rows: " + row + " # of col: " + col); 

    while (readFile.hasNextLine()) 
    { 
     //System.out.println(readFile.next()); 

     for (int i=0; i<row; i++) 
     { 
      String symbol = readFile.nextLine(); 

      for (int j=0; j<col; j++) 
      { 
       path = symbol.charAt(0); 
       //safe = symbol.charAt(maze[0][0]); 
       maze[i][j] = symbol.charAt(j); 
       if (maze[i][j] == path) 
       { 
        maze[i][j] = 1; 
       } 
       else 
       { 
        maze[i][j] = 0; 
       } 
       System.out.println("for loop Path coord = (" + i + " , " + j + ") Value at coord = " + maze[i][j]); 

      }//for j  
     }//for i 
    }// while 
    readFile.close(); 
    return maze; 
} 

txt.File的內容(迷宮)

7 12 
..+.+.++++++ 
.++...++...+ 
..++.....+.+ 
+.+..++.+..+ 
+...++....++ 
+.+++..++..+ 
++++++++++.. 

輸出 - RIGHT

for loop Path coord = (0 , 0) Value at coord = 1 
for loop Path coord = (0 , 1) Value at coord = 1 
for loop Path coord = (0 , 2) Value at coord = 0 
for loop Path coord = (0 , 3) Value at coord = 1 
for loop Path coord = (0 , 4) Value at coord = 0 
for loop Path coord = (0 , 5) Value at coord = 1 
for loop Path coord = (0 , 6) Value at coord = 0 
for loop Path coord = (0 , 7) Value at coord = 0 
for loop Path coord = (0 , 8) Value at coord = 0 
for loop Path coord = (0 , 9) Value at coord = 0 
for loop Path coord = (0 , 10) Value at coord = 0 
for loop Path coord = (0 , 11) Value at coord = 0 
for loop Path coord = (1 , 0) Value at coord = 1 
for loop Path coord = (1 , 1) Value at coord = 0 
for loop Path coord = (1 , 2) Value at coord = 0 
for loop Path coord = (1 , 3) Value at coord = 1 
for loop Path coord = (1 , 4) Value at coord = 1 
for loop Path coord = (1 , 5) Value at coord = 1 
for loop Path coord = (1 , 6) Value at coord = 0 
for loop Path coord = (1 , 7) Value at coord = 0 
for loop Path coord = (1 , 8) Value at coord = 1 
for loop Path coord = (1 , 9) Value at coord = 1 
for loop Path coord = (1 , 10) Value at coord = 1 
for loop Path coord = (1 , 11) Value at coord = 0 
for loop Path coord = (2 , 0) Value at coord = 1 
for loop Path coord = (2 , 1) Value at coord = 1 
for loop Path coord = (2 , 2) Value at coord = 0 
for loop Path coord = (2 , 3) Value at coord = 0 
for loop Path coord = (2 , 4) Value at coord = 1 
for loop Path coord = (2 , 5) Value at coord = 1 
for loop Path coord = (2 , 6) Value at coord = 1 
for loop Path coord = (2 , 7) Value at coord = 1 
for loop Path coord = (2 , 8) Value at coord = 1 
for loop Path coord = (2 , 9) Value at coord = 0 
for loop Path coord = (2 , 10) Value at coord = 1 
for loop Path coord = (2 , 11) Value at coord = 0 

輸出 - WRONG

for loop Path coord = (3 , 0) Value at coord = 1 //Value should = 0 because ="+" 
for loop Path coord = (3 , 1) Value at coord = 0 
+0

您可以發佈您正在使用的文件的內容嗎? – sunleo

+0

迷宮部分是文件的內容。 – handro

+0

我將其重命名爲文件內容以使其更容易。感謝您的幫助。 – handro

回答

1

你的錯誤是內部for循環中:

path = symbol.charAt(0); // <-- this is erroneous 
maze[i][j] = symbol.charAt(j); 
if (maze[i][j] == path) { 
    maze[i][j] = 1; 
} else { 
    maze[i][j] = 0; 
} 

您正在改變path場的每一行。因此,對於前三行,您將路徑設置爲.,因爲這是該行中的第一個字符。在第四行,第一個字符是+,所以結果出現翻轉。

您需要決定如何確定路徑字符,並且每個文件只做一次。如果你有信心,在整個迷宮中的第一個字符是路徑符號,然後閱讀一次,並將其存儲:

public int[][] readFile(String fileName) throws IOException { 
    readFile = new Scanner(new File(fileName)); 

    info = readFile.nextLine(); 
    rowCol = info.split(" ", 2); 
    row = Integer.parseInt(rowCol[0]); 
    col = Integer.parseInt(rowCol[1]); 
    maze = new int[row][col]; 
    System.out.println("# of rows: " + row + " # of col: " + col); 

    int row = 0; 

    while (readFile.hasNextLine()) { 
    String symbol = readFile.nextLine(); 

    if (row == 0) { 
     path = symbol.charAt(0); 
    } 

    for (int j = 0; j < col; j++) { 
     maze[row][j] = symbol.charAt(j); 
     if (maze[row][j] == path) { 
     maze[row][j] = 1; 
     } else { 
     maze[row][j] = 0; 
     } 
     System.out.println("Path coord = (" + row + " , " + j 
      + ") Value at coord = " + maze[row][j]); 

    }// for j 

    row++; 

    }// while 
    readFile.close(); 
    return maze; 
} 

我也糾正了循環結構。在您的原始示例中,您的while循環無效,只循環一次。您的for循環正在完成所有工作(未檢查是否有要讀取的行)。

其他意見:

  • 目前你的類可以簡化成一個工具類用一個靜態方法(雖然我明白,這可能在未來改變)。

  • 您有臨時變量(例如row,colreadFile)作爲字段存儲在您的班級中;他們應該是你的方法中的變量。

  • 您似乎將二進制值(10)存儲在數組中,但是作爲整數存儲。你可以考慮返回一個布爾數組。

+0

我真的很感激你花時間來解釋。這些變化奏效了,我會考慮將數組切換爲布爾值與整數。 – handro

+0

我將行,列和readfile作爲字段存儲在我的班級中,我可以從其他班級訪問它們。但是我會忽略閱讀文件,因爲它沒有意義。 – handro