0

I'm trying to read a text file called input.in an array of characters representing a map. The file contains two integers N and numLifes, and the corresponding matrix. N represents the dimension of a square matrix (NxN).

I want that if N is less than 10, assign to the variable N, the value of 10 and fill the matrix 'mVill' with a '#', and then read the map file and integrate it with the matrix filled with '#'. Heres the code:

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

For this input:

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

the output is (which is wrong):

F..*..F###

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

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

The output should I expect to receive it:

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

What am I doing wrong? I do not see any error in reading the file.

2 Answers 2

1

Following Steve's answer, the quick and dirty way to correct it would be to do this when you are reading your characters:

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

So the linefeed and carriage return characters would be skipped.

Sign up to request clarification or add additional context in comments.

Comments

0

I think you're not taking the carriage-return / linefeed characters into account when reading the input. I would probably use a different technique, where you read a line at a time rather than a single character.

Try testing the character to see if it is a cr/lf and skip it if so.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.