I have a loadFromFile methods that loads in the file correctly but I can't figure out how to scan the first line in the file, store that in a variable then scan the rest using a delimiter and a while loop. The first value in the text file is a size for a maze and the rest is to set up walls and a mouse and cheese in the maze.
Here is the text file for reference.
6
0,0,m
5,2,c
0,1,w
1,1,w
1,3,w
1,4,w
1,5,w
2,1,w
2,3,w
2,4,w
2,5,w
4,1,w
4,2,w
4,3,w
4,4,w
5,1,w
My code so far is
private void loadFromFile(String filename) throws IOException { Scanner fileReader = new Scanner(new File(filename)); String info = fileReader.nextLine(); Scanner lineReader = new Scanner(info); int size = Integer.parseInt(lineReader.next()); maze = new Cell[size][size]; mazeSize = size; for (int r = 0; r < getSize(); r++) for (int c = 0; c < getSize(); c++) { maze[r][c] = new Cell(CellType.Open); } while (fileReader.hasNextLine()) { lineReader.useDelimiter(","); int r = Integer.parseInt(lineReader.next()); int c = Integer.parseInt(lineReader.next()); String type = lineReader.next(); type = type.trim(); if (type.equalsIgnoreCase("m")) { mousePosRow = r; mousePosCol = c; } if (type.equalsIgnoreCase("w")) { maze[r][c].setType(CellType.Wall); } if (type.equalsIgnoreCase("c")) { maze[r][c].setType(CellType.Cheese); } }
The while loop works if all the lines dealing with size are removed as well as the size from the text file. I am trying to store size then use it to create my 2D array of cells and set them all to open to make sure values are placed. Then based on the type specified in the 3rd spot set them to either walls, cheese, or mouse. This method is used in the maze constructor. If you need any clarification please ask and thank you so much!
PeskyToaster