0
2次元配列形式をファイルからプログラムに読み込む際に問題があります。このような書式を読んで、それぞれの文字をHvitRuteやSvartRuteのような文字( '#'または '。')に応じて格納することになっています。どちらもRuteを拡張しています。2次元配列形式をJavaのテキストファイルから正しく読み取ることができません
フォーマット:
13 13
#############
..#.........#
#.#######.###
#...#...#.#.#
###.#.#.###.#
#.#.#.#.....#
#.#.#.#######
#...#.......#
#.#########.#
#.#.........#
#.#.#######.#
#...#........
#############
行方不明イム私のアルゴリズムに任意の論理エラーがありますか?ありがとう! //コードをここに
Rute[][] labyrinth;
public void readFromFile(File file) throws Exception{
Scanner in = new Scanner(file);
String rows = in.nextLine();
String[] size = rows.split(" ");
int y = Integer.parseInt(size[0]); int x = Integer.parseInt(size[1]);
labyrinth = new Rute[y][x];
int index = 0;
while(in.hasNextLine()){
String inn = in.nextLine();
int secondIndex = 0;
for(int i = secondIndex; i < y; i++){
switch(inn.charAt(secondIndex)){
case '#': labyrint[index][secondIndex] = new HvitRute('#');
break;
case '.': labyrint[index][secondIndex] = new SvartRute(' ');
break;
default : System.out.println("Feil symbol");
break;
}
secondIndex++;
}
index++;
}
for(int i = 0; i < y; i++){
for(int k = 0; i < x; i++){
if(labyrinth[i][k] != null){
System.out.print(labyrinth[i][k].getType());
} else {
System.out.print("Error");
}
System.out.println();
}
}
} }
ながらループの前のprintlnにxとyを使用してチェックしても、旅館の文字列値を確認してください –
for文でsecondIndexを使用することは意味がありません。いつも0から始めるべきではないでしょうか? – tak3shi
ええ、ちょうどint i = 0、ありがとう。 –