简体   繁体   中英

How to convert a multi-line StringBuilder to a character array?

My requirement came when I was doing a chess problem wherein 8X8 character values were given through System.in. I was testing it and all the time I was giving 64 inputs that was pretty hard. Now, I wanted to keep the same in a text file and read it and store it in a character array. Please help me to do so. There are multiple ways those just read and display the contents of the file or we can convert into a 1D char array. But, I was wondering that it can be directly converted from a StringBuilder to a 2D character array.!!! Here is what I have tried.

StringBuilder c = new StringBuilder();
        File f = new File("file\\input.txt");
        FileInputStream br = new FileInputStream(f);
        int str;
        while ((str = br.read()) != -1) {
            c.append((char) str);
        }
        br.close();
        System.out.println(c);

        int strBegin = 0;

        for (int i = 0; i < input.length; i++) {
            for (int j = 0; j < input.length; j++) {
                input[i][j] = c.substring(strBegin, strBegin + 1).toCharArray()[0];
                strBegin++;
            }
        }
        for (int i = 0; i < input.length; i++) {
            for (int j = 0; j < input.length; j++) {
                System.out.print(input[i][j] + " ");
            }
            System.out.println();
        }

Here, the contents of the file input.txt:

 2345678
1 345678
12 45678
123 5678
1234 678
12345 78
123456 8
1234567 

Note: There is a diagonal space that also has to be stored into the array.

When I run the code, I get this:

 2345678
1 345678
12 45678
123 5678
1234 678
12345 78
123456 8
1234567 
  2 3 4 5 6 7 8 

 1   3 4 5 6 
  8 
 1 2   4 
  6 7 8 
 1 2 
    5 6 7 8 

1 2 3 4   6 7 8 

 1 2 3 4 5   
  8 
 1 2 3 4 

Rather than reading character by character I suggest you read a whole line directly and convert it to an array of char afterwards

public static char[][] readChessFile(String filename) throws IOException {
  char[][] input = new char[8][8];
  try (BufferedReader bufferedReader = new BufferedReader(new FileReader(filename))) {

    String line;
    for (int i = 0; i < input.length; i++) {
      line = bufferedReader.readLine();
      if (line == null || line.length() != 8) {
        throw new IllegalStateException("File is not in correct format");
      }
      input[i] = line.toCharArray();
    }
  }
  return input;
}

And here is my test code

try {
  char[][] result = readChessFile(filename);
  for (int i = 0; i < result.length; i++) {
    for (int j = 0; j < result[i].length; j++) {
      System.out.print(result[i][j]);
    }
    System.out.println();
  }
} catch (IOException e) {
  e.printStackTrace();
}

Here is one way to do it.

I allocate the first part of the two d array via

char[][] chars = new char[8][];

The rest will be assigned from the String.

      char[][] chars = new char[8][];
      try {
         BufferedReader br =
               new BufferedReader(new FileReader("input.txt"));
         String s;
         int i = 0;
         while ((s = br.readLine()) != null) {
           // assign the converted string array to chars[i]
            chars[i++] = s.toCharArray();
         }
         br.close();
      }
      catch (IOException ioe) {
         ioe.printStackTrace();
      }
      // now print them out, char by char.
      for (int i = 0; i < chars.length; i++) {
         for (int k = 0; k < chars[i].length; k++) {
            System.out.print(chars[i][k]);
         }
         System.out.println();
      }

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