简体   繁体   中英

Best way to process strings in Java

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. Eg 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 (ie, 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.

// Remember to handle whitespace chars in array
char[] row = "wbbbw bbbbb wwwww wbwbw bwbwb".toCharArray()

Everything else seems good.

Why not use an already existing text format such as JSON instead of inventing your own?

There are tons of JSON parsers out there that can read and write two dimensional arrays.

You get both the benefit of easy reading directly from the file(as with your original method) and the benefit of not having to parse an annoying string format.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class GameOfLife
{
    private String mFilename;
    private ArrayList<String> mLines;

    public GameOfLife(String filename)
    {
        mFilename = filename;
        read();
    }

    public char get(int x, int y)
    {
        String line = mLines.get(y);
        return line.charAt(x);
    }

    public void set(char c, int x, int y)
    {
        String line = mLines.get(y);
        String replacement = line.substring(0, x) + c + line.substring(x + 1, line.length());
        mLines.set(y, replacement);
    }

    private void read()
    {
        mLines = new ArrayList<String>();

        try
        {
            BufferedReader in = new BufferedReader(new FileReader(mFilename));
            String line = in.readLine();

            while (line != null)
            {
                mLines.add(line);
                line = in.readLine();
            }
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    private void write()
    {
        try
        {
            BufferedWriter out = new BufferedWriter(new FileWriter(mFilename));

            for (String line : mLines)
            {
                out.write(line + "\n");
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}

You can use String.toCharArray() for each line while reading the file.

char[][] board = new char[5][];
int i = 0;
while((line = buffRdr.readLine()) != null) {
    board[i++] = line.toCharArray();
}

And while writing either String.valueOf() or java.util.Arrays.toString() .

for(int i=0; i<board.length; i++) {
    //write Arrays.toString(board[i]);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM