我需要用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
您可以發佈您正在使用的文件的內容嗎? – sunleo
迷宮部分是文件的內容。 – handro
我將其重命名爲文件內容以使其更容易。感謝您的幫助。 – handro