Home > Software design >  How to convert txt file into 2d array in Java?
How to convert txt file into 2d array in Java?

Time:07-02

This is my first time working with arrays, especially with files in Java, and I had a problem how to output the data from a file to a 2d array.

So here i'm trying to construct a map for the game with using these data and keep it to 2D array. How to do it?

My data in the file:

8
2 0 0 1 1 0 0 0
0 0 1 0 0 1 0 0
0 0 0 0 0 0 0 0
1 0 1 0 0 1 0 1
1 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0
0 0 1 0 0 1 0 0
0 0 0 1 1 0 0 0

Error that i got:

File file The type of the expression must be an array type but it resolved to File


File file = new File("map1.txt");
        private final char[] allowed = {'0', '1', '2'};
        public List<Position> pPos = new ArrayList<>();
        Map(Scanner scan) throws InvalidMapException {
            List<String> strings = new ArrayList<>();
            int size = 8;
            boolean isValid = false;
            for(int i = 0; i < size; i  ) {
                strings.add(size);
                for(char ch : allowed) {
                    if(strings.get(strings.size() - 1).charAt(0) == ch) {
                        isValid = true;
                    }
    
                }
            
                if(!isValid) {
                    throw new InvalidMapException("Illegal character");
                }
    
            }
                isValid = false;
                if(strings.size() != size) {
                    throw new InvalidMapException("Map error");
                }
            isValid = false;
            int chCounter = 0;
            int xCounter = 0;
            int yCounter = 0;
            for(String string : strings) {
                for(char ch : strings.toCharArray()) {
                    if(ch == ' ') {
                        continue;
                    }
                    if(ch == '2') {
                        pPos.add(new Position(xCounter, yCounter));
    
                    }
                    chCounter  ;
                    for(char aCh: allowed) {
                        if(ch == allowed) {
                            isValid = true;
                            break; 
                        }
    
                    }
                    if(!isValid) {
                        throw new InvalidMapException("character error");
                    }
                    isValid = false;
                    chCounter  ;
    
                }
                if(chCounter != strings.size()) {
                    System.out.println(chCounter   " "   strings.size());
                    throw new InvalidMapException("size error");
                }
                chCounter = 0;
                yCounter  ;
                xCounter = 0;
    
            }
            file = new char[strings.size()][strings.size()];
            
            int i = 0; int j = 0;
            for(String string : strings) {
                for(char ch : string.toCharArray()) {
                    if(ch != ' ') {
                        file[i][j] = ch; //Here is the error.
                        j  ;
                    }
                }
            }
    
        }

CodePudding user response:

You are doing this very complicated.

Read all lines of your file:

List<String> lines = new ArrayList<>();
BufferedReader br = null;

try {
    br = new BufferedReader(new FileReader(fileName));
    String line;
        while ((line = br.readLine()) != null) {
            lines.add(line);
        }
}
catch (IOException e) {
        e.printStackTrace();
} finally {
    if (br != null) {
        br.close();
    }
}

Then split the lines with String.split(" "). You will get an array with all numbers per line. Store them in a list.

After that you can create a 2d array out of the items.

CodePudding user response:

Hi Variable file is of type java.io.File. You are attempting to assign a char array to it which is not possible. You would need to use something like this

char[][] charArray = new char[strings.size()][strings.size()];

Regardless, I would suggest you to follow the approach suggested by David instead of the complicated code that you have posted.

  • Related