I want make a game of life clone using text files to specify my starting board, then write the finished board to a new text file in a similar format. E.g. load this:
Board in:
wbbbw bbbbb wwwww wbwbw bwbwb
from a file, then output something like this:
Board out:
wbbbw bbbbb wwwww wwwww bwbwb
I do this by making a 2D array (char[][] board) of characters, reading the file line by line into a string, and using String.charAt() to access each character and store it in the array.
Afterward, I convert each element of board (i.e., board[0], board[1], etc.), back to a string using String.valueOf(), and write that to a new line in the second file.
Please tell me I’m an idiot for doing this and that there is a better way to go through the file -> string -> array -> string -> file process.
You can use
String.toCharArray()for each line while reading the file.And while writing either
String.valueOf()orjava.util.Arrays.toString().