扫描仪未按预期计算空格

我目前正在尝试使用扫描仪为推箱子游戏创建求解器,我现在遇到的问题是我的扫描仪没有按预期工作,我通常有一个文本文件,看起来像这样:


## ###### #

##### #####

当我尝试使用我的代码时,我想扫描每个字符,包括空格,保存它们,创建一个二维数组,把它们放在那里,这是我的第一步。问题是我的扫描仪没有像我希望的那样工作,我使用以下行:


sc.useDelimiter("s*");

这是我对空格计数的尝试,似乎确实如此,问题只是它每次进入空格时都会切换行。我的代码现在看起来像:


    package soko;


    import java.util.*;



    import java.io.*;


    public class Dowork {

        File file;

        Scanner sc;

        char a;

        static int lines;

        Scanner lineScanner;

        static int maxChar;

        ArrayList array;

        char[][] array2;

        Scanner bc;

        int n;

        int m;

        static int lines2;



    public Dowork() throws Exception{

        //lines += 1;

        file = new File("C:/Users/joaki/Desktop/sokoban/readin.txt");

        sc = new Scanner(file);

        bc = new Scanner(file);

        sc.reset();

        bc.reset();

        sc.useDelimiter("s*");

        ArrayList<Character> array = new ArrayList<Character>();

        while(sc.hasNext()) {

            String line = sc.next();

            if(maxChar <= line.length()) {

                maxChar = line.length();

            }

            for (char ch:line.toCharArray()) {

                array.add(ch);

            }

            if(sc.hasNextLine()) {

                lines++;

            }


        }

        sc.close();

        while(bc.hasNextLine()) {

            lines2++;

        }

        bc.close();

        array2 = new char[lines][maxChar];

        Iterator<Character> itemIterator = array.iterator();

            while(itemIterator.hasNext()) {

                itemIterator.next();

                for (int q = 0; q < lines; q++) {

                    for(int r = 0; r < maxChar; r++) {

                        Character j = itemIterator.next();

                        array2[q][r] = j;


                    }

                }


            }


举个例子,当我使用具有以下内容的 txt 文件时:


########

#   # .#

#   $$.#

####   #

   #@ ##

   ####

它数到 56 行,显然不应该是 56。我哪里做错了?


德玛西亚99
浏览 164回答 1
1回答

慕的地6264312

要记住的主要编程规则之一:保持简单在这一点上,为什么不简单地使用一个普通的扫描仪,一个没有改变分隔符的扫描仪,并使用它来获取文件中的行和行。然后,您可以根据需要处理行,例如转换为字符数组,或者在需要时使用自己的行扫描仪解析它。List<String> lines = new ArrayList<>();while (sc.hasNextLine()) {&nbsp; &nbsp; lines.add(sc.nextLine());}array2 = new char[lines.size()][];for (int i = 0; i < array2.length; i++) {&nbsp; &nbsp; array2[i] = lines.get(i).toCharArray();}例如:import java.io.InputStream;import java.util.ArrayList;import java.util.Formatter;import java.util.List;import java.util.Scanner;public class DoWork2 {&nbsp; &nbsp; // assuming data in data.txt file in same path as class files&nbsp; &nbsp; private static final String RESRC_PATH = "data.txt";&nbsp; &nbsp; private char[][] array2;&nbsp; &nbsp; public DoWork2(String resrcPath) {&nbsp; &nbsp; &nbsp; &nbsp; InputStream is = getClass().getResourceAsStream(resrcPath);&nbsp; &nbsp; &nbsp; &nbsp; Scanner sc = new Scanner(is);&nbsp; &nbsp; &nbsp; &nbsp; List<String> lines = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; while (sc.hasNextLine()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lines.add(sc.nextLine());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; array2 = new char[lines.size()][];&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < array2.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array2[i] = lines.get(i).toCharArray();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (sc != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sc.close();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public String getLines2() {&nbsp; &nbsp; &nbsp; &nbsp; StringBuilder sb = new StringBuilder();&nbsp; &nbsp; &nbsp; &nbsp; Formatter formatter = new Formatter(sb);&nbsp; &nbsp; &nbsp; &nbsp; for (char[] cs : array2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; formatter.format("%s%n", new String(cs));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; formatter.close();&nbsp; &nbsp; &nbsp; &nbsp; return sb.toString();&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; DoWork2 doWork2 = new DoWork2(RESRC_PATH);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(doWork2.getLines2());&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java