扫描输入并跳过某些字符

发布于 01-06 18:59 字数 1924 浏览 4 评论 0原文

我正在扫描一个文本文件,其中有一个带有几个分隔符的数独板。这就是示例输入的样子。

1 - - | 4 5 6 | - - -  
5 7 - | 1 3 b | 6 2 4  
4 9 6 | 8 7 2 | 1 5 3  
======+=======+======  
9 - - | - - - | 4 6 -  
6 4 1 | 2 9 7 | 8 3 -  
3 8 7 | 5 6 4 | 2 9 -  
======+=======+======     
7 - - | - - - | 5 4 8   
8 r 4 | 9 1 5 | 3 7 2   
2 3 5 | 7 4 $ | 9 1 6  

其中有“|”的地方作为边框,====+====+==== 作为分隔线。我编写此代码是为了忽略 |和 ===+====+=== 但它跳过了这部分代码并将它们声明为无效字符并在其中添加 0

public static int [][] createBoard(Scanner input){

    int[][] nSudokuBoard = new int[9][9];

  for (rows = 0; rows < 9; rows++){

        for (columns = 0; columns < 9; columns++){

            if(input.hasNext()){

                if(input.hasNextInt()){
                    int number = input.nextInt();
                    nSudokuBoard[rows][columns] = number;
               }//end if int

                else if(input.hasNext("-")){
                    input.next();
                    nSudokuBoard[rows][columns] = 0;
                    System.out.print("Hyphen Found \n");
                    }//end if hyphen

                else if(input.hasNext("|")){
                    System.out.print("border found \n");
                    input.next();

                }// end if border
                else if(input.hasNext("======+=======+======")){
                    System.out.print("equal row found \n");
                    input.next();
                }// end if equal row

               else {
                   System.out.print("Invalid character detected at... \n Row: " + rows +" Column: " + columns +"\n");
                   System.out.print("Invalid character(s) replaced with a '0'. \n" );
                   input.next();
               }//end else

            }//end reading file
       }//end column for loop
    }//end row for looop
 return  nSudokuBoard;
}//end of createBoard

我与导师讨论过,但我不记得他关于如何进行的建议解决这个问题。

I'm scanning a text file that has a sudoku board with a few separators. this is what a sample input would look like.

1 - - | 4 5 6 | - - -  
5 7 - | 1 3 b | 6 2 4  
4 9 6 | 8 7 2 | 1 5 3  
======+=======+======  
9 - - | - - - | 4 6 -  
6 4 1 | 2 9 7 | 8 3 -  
3 8 7 | 5 6 4 | 2 9 -  
======+=======+======     
7 - - | - - - | 5 4 8   
8 r 4 | 9 1 5 | 3 7 2   
2 3 5 | 7 4 $ | 9 1 6  

where it has "|" as borders and =====+====+==== as dividers. I made this code to ignore the | and ====+===+=== but it's skipping that part of the code and declaring them as invalid characters and adding 0's in there place

public static int [][] createBoard(Scanner input){

    int[][] nSudokuBoard = new int[9][9];

  for (rows = 0; rows < 9; rows++){

        for (columns = 0; columns < 9; columns++){

            if(input.hasNext()){

                if(input.hasNextInt()){
                    int number = input.nextInt();
                    nSudokuBoard[rows][columns] = number;
               }//end if int

                else if(input.hasNext("-")){
                    input.next();
                    nSudokuBoard[rows][columns] = 0;
                    System.out.print("Hyphen Found \n");
                    }//end if hyphen

                else if(input.hasNext("|")){
                    System.out.print("border found \n");
                    input.next();

                }// end if border
                else if(input.hasNext("======+=======+======")){
                    System.out.print("equal row found \n");
                    input.next();
                }// end if equal row

               else {
                   System.out.print("Invalid character detected at... \n Row: " + rows +" Column: " + columns +"\n");
                   System.out.print("Invalid character(s) replaced with a '0'. \n" );
                   input.next();
               }//end else

            }//end reading file
       }//end column for loop
    }//end row for looop
 return  nSudokuBoard;
}//end of createBoard

I talked it over with a tutor but i don't remember his suggestion on how to fix this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月苍老的讽刺2025-01-13 18:59:45

hasNext 的 String 参数被视为正则表达式。您需要转义特殊字符:

else if(input.hasNext("\\|")){

else if(input.hasNext("======\\+=======\\+======")){

http://ideone.com/43j7R

The String parameter to hasNext is treated as a regular expression. You need to escape special characters:

else if(input.hasNext("\\|")){

else if(input.hasNext("======\\+=======\\+======")){

http://ideone.com/43j7R

感悟人生的甜2025-01-13 18:59:45

即使您正在使用边框或分隔线,您的循环也会增加行计数器和列计数器。因此,即使您解决了其他问题(如前面的答案中所述),您也将在阅读所有输入之前完成填充矩阵。您需要修改代码,以便仅在消耗完整数或短划线后有条件地推进行计数器和列计数器。这意味着删除 for 循环,将第一个 if(input.hasNext()) 更改为 while,并添加 rows++columns++ 在您使用整数或短划线并为 nSudokuBoard[rows][columns] 设置值的位置。您还需要逻辑来确定何时增加以及何时将设置回0

另外,从风格上来说,您应该将 rows 重命名为 row,将 columns 重命名为 column

Your loops increment the row and column counters even when you are consuming a border or a divider. So even after you fix other issues (as explained in the previous answer), you will finish filling the matrix before you've read all your input. You need to modify your code to conditionally advance the row and column counters only after you've consumed an integer or a dash. This would mean removing the for loops, changing the first if(input.hasNext()) to a while, and adding rows++ and columns++ in the places where you've consumed an integer or dash and are setting a value for nSudokuBoard[rows][columns]. You would also need logic to determine when to increment rows and when to set columns back to 0.

Also, stylistically speaking, you should rename rows to row and columns to column.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文