0

我正在尝试读取一个名为 input.in 的文本文件,它是一个表示地图的字符数组。该文件包含两个整数 N 和 numLifes,以及相应的矩阵。N 表示方阵的维数 (NxN)。

我希望如果 N 小于 10,则将 10 的值赋给变量 N,并用“#”填充矩阵“mVill”,然后读取映射文件并将其与用“#”填充的矩阵集成. 继承人的代码:

import java.io.*;
import java.io.FileReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class hola {

    public static void main(String[] args) {
        char mVill[][] = null;
        int N, i,j, numLifes;
        String line=null;
        StringTokenizer tk;
        char caract;
        FileInputStream fstream = null;
        try {
            fstream = new FileInputStream("C:/input.in");
        } catch (FileNotFoundException e) {
            System.exit(-1);
        }

        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));

        try {
            if ((line = br.readLine()) == null) {
                System.out.print("Error empty file...");
                System.exit(0);
            }
        } catch (IOException e) {
        }

        tk = new StringTokenizer(line);
                N = Integer.parseInt(tk.nextToken());
                numLifes = Integer.parseInt(tk.nextToken());

                int nAux=N;
                if (N<10){
                    N=10;
                    mVill = new char[N][N];
                    for (char[] row: mVill)
                        Arrays.fill(row, '#');
                    for (i=0; i <nAux; i++) {
                        for (j=0;j<nAux;j++){
                            try{
                            caract = (char) br.read();
                            mVill[i][j]=caract;
                            }catch (Exception e){
                               System.out.println("Error in read file");
                            }
                        }
                    }

                }else{
                    mVill = new char[N][N];
                    for (i = 0; i < N; i++) {
                        try {
                            mVill[i] = br.readLine().toCharArray();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                        }
                    }
                }
                System.out.println(N+" "+numLifes);

                for (i = 0; i < N; i++) {
                    for (j = 0; j < N; j++) {
                        System.out.print(mVill[i][j]);
                    }
                    System.out.println();
                }
    }//end main
}//end class

对于此输入:

7 3
F..*..F
.##.##.
.#...#.
*..P..*
.#...#.
.##.##.
F..*..F

输出是(这是错误的):

F..*..F###

.##.####
#.
.#.###
..#.
*###
..P..*
###

.#...####
.
.##.###
##########
##########
##########

我应该期望收到的输出:

F..*..F###
.##.##.###
.#...#.###
*..P..*###
.#...#.###
.##.##.###
F..*..F###
##########
##########
##########

我究竟做错了什么?我在读取文件时没有看到任何错误。

4

2 回答 2

1

按照史蒂夫的回答,纠正它的快速而肮脏的方法是在阅读角色时这样做:

caract = (char) br.read();
while (caract == '\n' || caract == '\r') {
    caract = (char) br.read();
}
mVill[i][j]=caract;

因此将跳过换行符和回车符。

于 2013-01-31T03:24:46.817 回答
0

我认为您在阅读输入时没有考虑回车/换行字符。我可能会使用不同的技术,一次读取一行而不是一个字符。

尝试测试该字符以查看它是否为 cr/lf,如果是则跳过它。

于 2013-01-31T03:17:19.880 回答