如何使用Java扫描仪读取每个特定单词?

如何使用Java扫描仪读取每个特定单词?,java,arrays,java.util.scanner,Java,Arrays,Java.util.scanner,你好,我有文件名保存,为保存游戏在我的Java程序。这个文件是这样的 = B . . . . . W . B . W . B . . . B W W B . . . . . == 1 0 0 0 0 0 1 0 1 0 1 0 1 0 0 0 1 1 1 1 0 0 0 0 0 === 9 ==== 2 ===== 5 我很困惑,如何读取这个文件,并将每个特定文件的值存储到2D数组和其他变量中, 目前我有5个变量 arrMove[5][5], arrProc[5][5], forTurn, s

你好,我有文件名保存,为保存游戏在我的Java程序。这个文件是这样的

=
B
.
.
.
.
.
W
.
B
.
W
.
B
.
.
.
B
W
W
B
.
.
.
.
.
==
1
0
0
0
0
0
1
0
1
0
1
0
1
0
0
0
1
1
1
1
0
0
0
0
0
===
9
====
2
=====
5
我很困惑,如何读取这个文件,并将每个特定文件的值存储到2D数组和其他变量中, 目前我有5个变量

arrMove[5][5], arrProc[5][5], forTurn, seconds, totalSeconds
我有loadgame的代码,目前我只能从这个文件中存储一个数组
这是错误的

int x = 0;
int y = 0;
Scanner inFile1 = new Scanner(new File("save.txt"));
while (inFile1.hasNext()) {
// find next line
    if (inFile1.hasNext("=")){
        ActivityBoard.arrMove[x][y] = inFile1.next();
        y++;
        if (y > 4) {
            y = 0;
            x++;
        }
    }
}
inFile1.close();
这是我的储蓄游戏

if (e.getSource().equals(SaveMenu)) {
            try {
                PrintWriter out = new PrintWriter("save.txt");
                out.println("=");
                for (int i = 0; i < rows; i++) {
                    for (int j = 0; j < cols; j++) {
                        out.println(ActivityBoard.arrMove[i][j]);
                    }
                }
                out.println("==");
                for (int i = 0; i < rows; i++) {
                    for (int j = 0; j < cols; j++) {
                        out.println(ActivityBoard.arrProc[i][j]);
                    }
                }
                out.println("===");
                out.println(forTurn);
                out.println("====");
                out.println(seconds);
                out.println("=====");
                out.println(TotalTimer.getTime());
                out.close();
                this.dispose();
                System.exit(1);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(ActivityBoard.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
if(例如getSource().equals(保存菜单)){
试一试{
PrintWriter out=新的PrintWriter(“save.txt”);
out.println(“=”);
对于(int i=0;i
我想把这个值存储到我的变量中
从分隔符
=
=

我想存储到数组
arrMove[5][5]


从分隔符
=
==

我想存储到数组
arrProc[5][5]


从分隔符
==
==

我想存储到
forTurn


来自分隔符
===

我想存储到

有人能帮我吗?谢谢:)
sry用于我糟糕的英语。

这不是一种很好的文件格式,但它很容易像这样解析:

public static void main(String[] args) throws Exception {
    String s;
    Scanner scanner = new Scanner(new File("c:/temp/x.txt"));
    while( ! "=".equals((s = scanner.next()))) {
        // Ignore - getting to start
    }

    while( ! "==".equals((s = scanner.next()))) {
        // Put in first array
        System.out.println("First section: " + s);
    }

    while( ! "===".equals((s = scanner.next()))) {
        // Put in first array
        System.out.println("Second section: " + s);
    }

    // etc...
}

你是存储了那个文件还是正在使用第三方格式?我正在存储那个文件,先生,在我的程序中,状态机可能会有所帮助,但我想,对于新手来说,这太“沉重”了。如果这是你的文件,那么试着找一种更好的文件格式,这样更容易再次阅读。你能给我推荐一种更好的文件存储格式吗?先生,我很困惑,我是java新手,从google我就有了这段代码来保存和加载一个
BufferedReader
with
readLine()
Scanner
更容易。它看起来像图中所示,图中有点和更多。哇,这很有效谢谢你,先生,我花了3个小时来解决这个问题,我的头撞到墙上,你在几分钟内就解决了这个问题:D,非常感谢先生:*