Java 如何读入多个基于文本的拼字板,并将其放入2D数组中?

Java 如何读入多个基于文本的拼字板,并将其放入2D数组中?,java,collections,java.util.scanner,Java,Collections,Java.util.scanner,我目前正在尝试制作一个拼字游戏解算器,我在读取代表棋盘的文本文件时遇到了一些问题。例如,我有这样一个文件: 15 3. .. .. .2 .. .. .. 3. .. .. .. .2 .. .. 3. .. 2. .. .. .. .3 .. .. .. .3 .. .. .. 2. .. .. .. 2. .. .. .. .2 .. .2 .. .. .. 2. .. .. .2 .. .. 2. l a u d a t o r .. .. .2 .. .. .. .. 2

我目前正在尝试制作一个拼字游戏解算器,我在读取代表棋盘的文本文件时遇到了一些问题。例如,我有这样一个文件:

15
3. .. .. .2 .. .. .. 3. .. .. .. .2 .. .. 3.
.. 2. .. .. .. .3 .. .. .. .3 .. .. .. 2. ..
.. .. 2. .. .. .. .2 .. .2 .. .. .. 2. .. ..
.2 .. .. 2.  l  a  u  d  a  t  o  r .. .. .2
.. .. .. .. 2. .. ..  h .. .. 2. .. .. .. ..
.. .3 .. .. .. .3 ..  o .. .3 .. .. .. .3 ..
.. .. .2 .. .. .. .2  t .2 .. .. .. .2 .. ..
3. .. .. .2 .. .. ..  i .. .. .. .2 .. .. 3.
.. .. .2 .. .. .. .2 .. .2 .. .. .. .2 .. ..
.. .3 .. .. .. .3 .. .. .. .3 .. .. .. .3 ..
.. .. .. .. 2. .. .. .. .. .. 2. .. .. .. ..
.2 .. .. 2. .. .. .. .2 .. .. .. 2. .. .. .2
.. .. 2. .. .. .. .2 .. .2 .. .. .. 2. .. ..
.. 2. .. .. .. .3 .. .. .. .3 .. .. .. 2. ..
3. .. .. .2 .. .. .. 3. .. .. .. .2 .. .. 3.
le*mdoe
7
3. .. .. 2. .. .. 3.
.. .3 .. .. .. .3 ..
.. ..  a  d .2 .. ..
2. ..  u  h .. .. 2.
.. ..  l  o .2 .. ..
..  m  a  t .. .3 ..
 r  e  S  i  d .. 3.
toloeri
21
4. .. .. .2 .. .. .. 3. .. .. .2 .. .. 3. .. .. .. .2 .. .. 4.
.. 2. .. .. .3 .. .. .. 2. .. .. .. 2. .. .. .. .3 .. .. 2. ..
.. .. 2. .. .. .4 .. .. .. 2. .. 2. .. .. .. .4 .. .. 2. .. ..
.2 .. .. 3. .. .. .2 .. .. .. 3. .. .. .. .2 .. .. 3. .. .. .2
.. .3 .. .. 2. .. .. .. .3 .. .. .. .3 .. .. .. 2. .. .. .3 ..
.. .. .4 .. .. 2. .. .. .. .2 .. .2 .. .. .. 2. .. .. .4 .. ..
.. .. .. .2 .. .. 2.  q  u  e  p .. .. .. 2. .. .. .2 .. .. ..
3. .. .. .. .. ..  l  i .. .. .. .. .. 2. .. .. .. .. .. .. 3.
..  g  l  i  t ..  a .. .3 .. .. .. .3 .. .. .. .3 .. .. 2. ..
.. ..  a .. .. .2  u ..  n .2 .. .2 .. .. .. .2 .. .. 2. .. ..
 d  a  w  d .. ..  d  h  o  t  i .. .. .. .2 .. .. 3. .. .. .2
.. ..  f  i  g  j  a  m  s .2 .. .2 .. .. .. .2 .. .. 2. .. ..
..  t  u  n  e ..  t ..  y .. .. .. .3 .. .. .. .3 .. .. 2. ..
 m  e  l  o  d  e  o  N .. .. .. .. .. 2. .. .. .. .. .. .. 3.
.. .. .. .2 .. ..  r .. .. .. .2 .. .. .. 2. .. .. .2 .. .. ..
..  v  e  z  i  r  s .. .. .2 .. .2 .. .. .. 2. .. .. .4 .. ..
.. .3 ..  o 2. .. .. .. .3 .. .. .. .3 .. .. .. 2. .. .. .3 ..
.2  b  a  r  f .. .2 .. .. .. 3. .. .. .. .2 .. .. 3. .. .. .2
.. ..  a  r  o  w .. .. .. 2. .. 2. .. .. .. .4 .. .. 2. .. ..
.. 2. ..  o  x  y .. .. 2. .. .. .. 2. .. .. .. .3 .. .. 2. ..
 k  e  p  s ..  n .. 3. .. .. .2 .. .. 3. .. .. .. .2 .. .. 4.
ntnbtoi
15
3. .. .. .2 .. .. .. 3. .. .. .. .2 .. .. 3.
.. 2. .. .. .. .3 .. .. .. .3 .. .. .. 2. ..
.. .. 2. .. .. .. .2 .. .2 .. .. .. 2. .. ..
.2 .. .. 2. .. .. .. .2 .. .. .. 2. .. .. .2
.. .. .. .. 2. .. .. .. .. .. 2. .. .. .. ..
.. .3 .. .. .. .3 .. .. .. .3 .. .. .. .3 ..
.. .. .2 .. .. .. .2 .. .2 .. .. .. .2 .. ..
3. .. .. .2 ..  c  a  t .. .. .. .2 .. .. 3.
.. .. .2 .. .. .. .2 .. .2 .. .. .. .2 .. ..
.. .3 .. .. .. .3 .. .. .. .3 .. .. .. .3 ..
.. .. .. .. 2. .. .. .. .. .. 2. .. .. .. ..
.2 .. .. 2. .. .. .. .2 .. .. .. 2. .. .. .2
.. .. 2. .. .. .. .2 .. .2 .. .. .. 2. .. ..
.. 2. .. .. .. .3 .. .. .. .3 .. .. .. 2. ..
3. .. .. .2 .. .. .. 3. .. .. .. .2 .. .. 3.
dgos*ie
//list of boards, store this outside of the method so that it can be accessed from other places like you main method etc
public static ArrayList<String[][]> boards = new ArrayList<>();

//Load the file into the boards array, or change "void" to "ArrayList<String[][]>" if you want to return the array directly from the method
public static void populateBoard(File file) throws FileNotFoundException {
    Scanner scan = new Scanner(file);
    int currentBoardSize = 0;
    String[][] currentBoard;
    
    //Get the very first board size so that the while loop works correctly
    String line = scan.nextLine();
    
    while(line != null) {       
        //Check it the first line has no spaces and is a single number indicating a new board
        if(line.contains(" ") == false) {   
            try {   
                //Get the board size
                currentBoardSize = Integer.parseInt(line);
            }
            catch (NumberFormatException ex) {
                System.out.println("Error, unable to read board size: ");
                ex.printStackTrace();
                break;
            }
            currentBoard = new String[currentBoardSize][currentBoardSize];
            System.out.println("Created new board that is "+currentBoardSize + "x"+currentBoardSize);
        
            //populate the borad using the next number of lines based on the board size
            for (int row = 0; row < currentBoardSize; row++) {
                ArrayList<String> input = new ArrayList<>(Arrays.asList(scan.nextLine().split(" ")));
                // Removes blank characters which occur because of the letters on the board having a space instead
                // of a dot to their left.
                input.removeIf(string -> string.equals(""));
                System.out.println(input.toString());

                for (int column = 0; column < input.size(); column++) {
                    currentBoard[row][column] = input.get(column);
                }
            }
        
            //Lastly do whatever you want with the word below each board
            String wordToSolve = scan.nextLine();
            System.out.println("Word to solve: "+wordToSolve );

            //Add this board to the list of boards
            boards.add(currentBoard);
        
            //Get the next board size, or break the while loop
            try {
                line = scan.nextLine();
            }
            catch (NoSuchElementException ex) {
                break;
            }
        }
        else {
            System.out.println("Error unexpected board size found: "+line);
            break;
        }
    }
}
电路板上方的第一行是行/列的数量。然后,很明显,董事会。然后是用来放置字符的手

我试图实现的是一种将每个板彼此分开并独立解决它们的方法。我不确定如何使用扫描仪,例如,在读取行/列编号后停止,创建相应的2D阵列,然后读入板并在手之前停止,然后读取手并将其放入手对象,然后继续下一块板。我不知道如何计算除数,如果这有意义的话

这是我的尝试,这是唯一有效的方法。我只能让它工作,如果我删除第一行,只有一个单板。顶部注释掉的行是我试图读取的第一行,并创建相应的数组,但每当我这样做时,对于我的
input
ArrayList,第一个sysout是一个空白列表,然后它才开始读取电路板本身,这对我来说没有任何意义

public static void populateBoard(File file) throws FileNotFoundException {
        Scanner scan = new Scanner(file);
//        String NUMBER_ROWS = scan.next();
//        System.out.println(NUMBER_ROWS);

//        board = new String[NUMBER_ROWS][NUMBER_ROWS];
        board = new String[15][15];
        int actualLine = 0;

        while(actualLine < 15) {
            ArrayList<String> input = new ArrayList<>(Arrays.asList(scan.nextLine().split(" ")));
            // Removes blank characters which occur because of the letters on the board having a space instead
            // of a dot to their left.
            input.removeIf(string -> string.equals(""));
            System.out.println(input.toString());

            for(int i = 0; i < input.size(); i++) {
                board[actualLine][i] = input.get(i);
            }
            actualLine++;
        }

    }
publicstaticvoidpopulateboard(文件)抛出FileNotFoundException{
扫描仪扫描=新扫描仪(文件);
//String NUMBER_ROWS=scan.next();
//系统输出打印项次(行数);
//board=新字符串[行数][行数];
board=新字符串[15][15];
int实际值=0;
而(实际值<15){
ArrayList输入=新的ArrayList(Arrays.asList(scan.nextLine().split(“”));
//删除因电路板上的字母有空格而出现的空白字符
//在他们的左边有一个点。
input.removeIf(字符串->字符串.equals(“”);
System.out.println(input.toString());
对于(int i=0;i

我觉得我没有很好地解释这一点,所以如果有什么我可以澄清的,请询问,我会这样做。

你大部分的方法都是这样的,你只需要将它包装在另一个循环中,找到电路板的大小,然后一次创建一个电路板

我们首先创建一个板的ArrayList,我们可以将每个板存储在
ArrayList boards=new ArrayList()中

假设文件总是正确布局,那么接下来我们需要创建一个循环,根据电路板的大小一次找到一个电路板
currentBoardSize=Integer.parseInt(line)我们可以一次创建一个
currentBoard=newstring[currentBoardSize][currentBoardSize]
然后我们处理下一个行数以匹配
currentBoardSize
,并将每一行存储到我们刚刚使用行拆分代码创建的2D数组中
ArrayList input=new ArrayList(Arrays.asList(scan.nextLine().split(“”))

最后,我们将整个电路板存储到
boards
列表
boards.add(currentBoard),然后我们让while循环重新开始,找到下一块板的大小,并创建一个新的2D阵列,直到文件中没有剩余的板为止

完整的示例可能如下所示:

15
3. .. .. .2 .. .. .. 3. .. .. .. .2 .. .. 3.
.. 2. .. .. .. .3 .. .. .. .3 .. .. .. 2. ..
.. .. 2. .. .. .. .2 .. .2 .. .. .. 2. .. ..
.2 .. .. 2.  l  a  u  d  a  t  o  r .. .. .2
.. .. .. .. 2. .. ..  h .. .. 2. .. .. .. ..
.. .3 .. .. .. .3 ..  o .. .3 .. .. .. .3 ..
.. .. .2 .. .. .. .2  t .2 .. .. .. .2 .. ..
3. .. .. .2 .. .. ..  i .. .. .. .2 .. .. 3.
.. .. .2 .. .. .. .2 .. .2 .. .. .. .2 .. ..
.. .3 .. .. .. .3 .. .. .. .3 .. .. .. .3 ..
.. .. .. .. 2. .. .. .. .. .. 2. .. .. .. ..
.2 .. .. 2. .. .. .. .2 .. .. .. 2. .. .. .2
.. .. 2. .. .. .. .2 .. .2 .. .. .. 2. .. ..
.. 2. .. .. .. .3 .. .. .. .3 .. .. .. 2. ..
3. .. .. .2 .. .. .. 3. .. .. .. .2 .. .. 3.
le*mdoe
7
3. .. .. 2. .. .. 3.
.. .3 .. .. .. .3 ..
.. ..  a  d .2 .. ..
2. ..  u  h .. .. 2.
.. ..  l  o .2 .. ..
..  m  a  t .. .3 ..
 r  e  S  i  d .. 3.
toloeri
21
4. .. .. .2 .. .. .. 3. .. .. .2 .. .. 3. .. .. .. .2 .. .. 4.
.. 2. .. .. .3 .. .. .. 2. .. .. .. 2. .. .. .. .3 .. .. 2. ..
.. .. 2. .. .. .4 .. .. .. 2. .. 2. .. .. .. .4 .. .. 2. .. ..
.2 .. .. 3. .. .. .2 .. .. .. 3. .. .. .. .2 .. .. 3. .. .. .2
.. .3 .. .. 2. .. .. .. .3 .. .. .. .3 .. .. .. 2. .. .. .3 ..
.. .. .4 .. .. 2. .. .. .. .2 .. .2 .. .. .. 2. .. .. .4 .. ..
.. .. .. .2 .. .. 2.  q  u  e  p .. .. .. 2. .. .. .2 .. .. ..
3. .. .. .. .. ..  l  i .. .. .. .. .. 2. .. .. .. .. .. .. 3.
..  g  l  i  t ..  a .. .3 .. .. .. .3 .. .. .. .3 .. .. 2. ..
.. ..  a .. .. .2  u ..  n .2 .. .2 .. .. .. .2 .. .. 2. .. ..
 d  a  w  d .. ..  d  h  o  t  i .. .. .. .2 .. .. 3. .. .. .2
.. ..  f  i  g  j  a  m  s .2 .. .2 .. .. .. .2 .. .. 2. .. ..
..  t  u  n  e ..  t ..  y .. .. .. .3 .. .. .. .3 .. .. 2. ..
 m  e  l  o  d  e  o  N .. .. .. .. .. 2. .. .. .. .. .. .. 3.
.. .. .. .2 .. ..  r .. .. .. .2 .. .. .. 2. .. .. .2 .. .. ..
..  v  e  z  i  r  s .. .. .2 .. .2 .. .. .. 2. .. .. .4 .. ..
.. .3 ..  o 2. .. .. .. .3 .. .. .. .3 .. .. .. 2. .. .. .3 ..
.2  b  a  r  f .. .2 .. .. .. 3. .. .. .. .2 .. .. 3. .. .. .2
.. ..  a  r  o  w .. .. .. 2. .. 2. .. .. .. .4 .. .. 2. .. ..
.. 2. ..  o  x  y .. .. 2. .. .. .. 2. .. .. .. .3 .. .. 2. ..
 k  e  p  s ..  n .. 3. .. .. .2 .. .. 3. .. .. .. .2 .. .. 4.
ntnbtoi
15
3. .. .. .2 .. .. .. 3. .. .. .. .2 .. .. 3.
.. 2. .. .. .. .3 .. .. .. .3 .. .. .. 2. ..
.. .. 2. .. .. .. .2 .. .2 .. .. .. 2. .. ..
.2 .. .. 2. .. .. .. .2 .. .. .. 2. .. .. .2
.. .. .. .. 2. .. .. .. .. .. 2. .. .. .. ..
.. .3 .. .. .. .3 .. .. .. .3 .. .. .. .3 ..
.. .. .2 .. .. .. .2 .. .2 .. .. .. .2 .. ..
3. .. .. .2 ..  c  a  t .. .. .. .2 .. .. 3.
.. .. .2 .. .. .. .2 .. .2 .. .. .. .2 .. ..
.. .3 .. .. .. .3 .. .. .. .3 .. .. .. .3 ..
.. .. .. .. 2. .. .. .. .. .. 2. .. .. .. ..
.2 .. .. 2. .. .. .. .2 .. .. .. 2. .. .. .2
.. .. 2. .. .. .. .2 .. .2 .. .. .. 2. .. ..
.. 2. .. .. .. .3 .. .. .. .3 .. .. .. 2. ..
3. .. .. .2 .. .. .. 3. .. .. .. .2 .. .. 3.
dgos*ie
//list of boards, store this outside of the method so that it can be accessed from other places like you main method etc
public static ArrayList<String[][]> boards = new ArrayList<>();

//Load the file into the boards array, or change "void" to "ArrayList<String[][]>" if you want to return the array directly from the method
public static void populateBoard(File file) throws FileNotFoundException {
    Scanner scan = new Scanner(file);
    int currentBoardSize = 0;
    String[][] currentBoard;
    
    //Get the very first board size so that the while loop works correctly
    String line = scan.nextLine();
    
    while(line != null) {       
        //Check it the first line has no spaces and is a single number indicating a new board
        if(line.contains(" ") == false) {   
            try {   
                //Get the board size
                currentBoardSize = Integer.parseInt(line);
            }
            catch (NumberFormatException ex) {
                System.out.println("Error, unable to read board size: ");
                ex.printStackTrace();
                break;
            }
            currentBoard = new String[currentBoardSize][currentBoardSize];
            System.out.println("Created new board that is "+currentBoardSize + "x"+currentBoardSize);
        
            //populate the borad using the next number of lines based on the board size
            for (int row = 0; row < currentBoardSize; row++) {
                ArrayList<String> input = new ArrayList<>(Arrays.asList(scan.nextLine().split(" ")));
                // Removes blank characters which occur because of the letters on the board having a space instead
                // of a dot to their left.
                input.removeIf(string -> string.equals(""));
                System.out.println(input.toString());

                for (int column = 0; column < input.size(); column++) {
                    currentBoard[row][column] = input.get(column);
                }
            }
        
            //Lastly do whatever you want with the word below each board
            String wordToSolve = scan.nextLine();
            System.out.println("Word to solve: "+wordToSolve );

            //Add this board to the list of boards
            boards.add(currentBoard);
        
            //Get the next board size, or break the while loop
            try {
                line = scan.nextLine();
            }
            catch (NoSuchElementException ex) {
                break;
            }
        }
        else {
            System.out.println("Error unexpected board size found: "+line);
            break;
        }
    }
}
上述给定的输出是
.2
,与预期一致