我的生命游戏代码中的字符串索引超出范围错误

时间:2015-10-21 16:16:44

标签: java indexoutofboundsexception conways-game-of-life

    package edu.bsu.cs121.mamurphy;

    import java.util.*;
    import java.io.*;
    import javax.swing.*;
    import java.awt.*;

// Maurice Murphy
// CS121
// 10/17/15

public class GameOfLifeMain extends JFrame {

    // Intitial reading and printing of the world

    public GameOfLifeMain() {
        super("Game of Life");
        setSize(600, 445);
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(1, 2));
        Test test1 = new Test();
        test1.setBackground(Color.LIGHT_GRAY);
        panel.add(test1);
        setContentPane(panel);
        setVisible(true);
        Temp one = new Temp(test1);
        one.start();
    }

    public static void main(String[] asd) {
        new GameOfLifeMain();
        System.out.println();

    }
}

class Temp extends Thread {
    Test anim;

    public Temp(Test anim) {
        this.anim = anim;

    }

    public void run()// for each instance of test begin will be executed
    {
        anim.begin();
    }
}

class Test extends JPanel

{
    final static int numOfRow = 25, numOfCol = 75;
    final static char DOT = '.';
    static char[][] grid = new char[numOfRow + 2][numOfCol + 2];
    static char[][] nextgrid = new char[numOfRow + 2][numOfCol + 2];
    boolean sameFlag;
    boolean blankFlag;

    public static void init(char[][] grid, char[][] nextgrid) {
        for (int numOfRow = 0; numOfRow <= numOfRow + 1; numOfRow++) {
            for (int numOfCol = 0; numOfCol <= numOfCol + 1; numOfCol++) {
                grid[numOfRow][numOfCol] = DOT;
                nextgrid[numOfRow][numOfCol] = DOT;
            }
        }
    }

    public static void pause() {
        try {
            Thread.currentThread();
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
        }
    }

    public void begin() {
        init(grid, nextgrid);
        read(grid);
        repaint(); // calls paintComponent
        pause();
        while (sameFlag == true && blankFlag == false) {
            nextGen(grid, nextgrid);
        }
    }

    public static void read(char[][] grid) {
        Scanner world = new Scanner(System.in);
        System.out.println("Type the file name of the world you'd like to create.");
        String fileName = world.nextLine();
        {
            try {
                world = new Scanner(new File(fileName));
            } catch (Exception ex) {
                System.out.println("Please insert a valid file name.");

            }
            ;

            for (int numOfRow = 1; numOfRow <= numOfRow; numOfRow++) {
                String s = world.next();
                for (int numOfCol = 1; numOfCol <= numOfCol; numOfCol++) {
                    grid[numOfRow][numOfCol] = s.charAt(numOfCol - 1);
                }

            }
        }
    }

    public void print(Graphics g) {
        int x, y;
        y = 20;
        for (int numOfRow = 1; numOfRow <= numOfRow; numOfRow++) {
            x = 20;
            for (int numOfCol = 1; numOfCol <= numOfCol; numOfCol++) {
                g.drawString("" + grid[numOfRow][numOfCol], x, y);
                x = x + 7;
            }
            y = y + 15;
        }
    }

    public static int neighbors(char[][] grid, int r, int c) {
        // counts the # of closest neighbors that are X's
        int count = 0;
        for (int numOfRow = r - 1; numOfRow <= r + 1; numOfRow++) {
            for (int numOfCol = c - 1; numOfCol <= c + 1; numOfCol++) {
                if (grid[numOfRow][numOfCol] == 'X') {
                    count++;
                }
            }
        }
        if (grid[r][c] == 'X') {
            count = count - 1;
        }
        return count;
    }

    public static void nextGen(char[][] grid, char[][] nextgrid) {
        for (int numOfRow = 1; numOfRow <= numOfRow; numOfRow++) {
            for (int numOfCol = 1; numOfCol <= numOfCol; numOfCol++) {
                if (grid[numOfRow][numOfCol] == 'X') {
                    int count = 0;
                    {
                        if (grid[numOfRow][numOfCol - 1] == 'X')
                            count = count + 1;
                        if (grid[numOfRow][numOfCol + 1] == 'X')
                            count = count + 1;
                        if (grid[numOfRow - 1][numOfCol] == 'X')
                            count = count + 1;
                        if (grid[numOfRow - 1][numOfCol - 1] == 'X')
                            count = count + 1;
                        if (grid[numOfRow - 1][numOfCol + 1] == 'X')
                            count = count + 1;
                        if (grid[numOfRow + 1][numOfCol - 1] == 'X')
                            count = count + 1;
                        if (grid[numOfRow + 1][numOfCol] == 'X')
                            count = count + 1;
                        if (grid[numOfRow + 1][numOfCol + 1] == 'X')
                            count = count + 1;
                    }

                    if (count == 2 || count == 3) {
                        nextgrid[numOfRow][numOfCol] = 'X';
                    } else
                        nextgrid[numOfRow][numOfCol] = DOT;
                }
                if (grid[numOfRow][numOfCol] == DOT) {
                    int count1 = 0;
                    {
                        if (grid[numOfRow][numOfCol - 1] == 'X')
                            count1 = count1 + 1;
                        if (grid[numOfRow][numOfCol + 1] == 'X')
                            count1 = count1 + 1;
                        if (grid[numOfRow - 1][numOfCol] == 'X')
                            count1 = count1 + 1;
                        if (grid[numOfRow - 1][numOfCol - 1] == 'X')
                            count1 = count1 + 1;
                        if (grid[numOfRow - 1][numOfCol + 1] == 'X')
                            count1 = count1 + 1;
                        if (grid[numOfRow + 1][numOfCol - 1] == 'X')
                            count1 = count1 + 1;
                        if (grid[numOfRow + 1][numOfCol] == 'X')
                            count1 = count1 + 1;
                        if (grid[numOfRow + 1][numOfCol + 1] == 'X')
                            count1 = count1 + 1;
                    }
                    if (count1 == 3)
                        nextgrid[numOfRow][numOfCol] = 'X';
                }
            }
        }
    }

    public static void copy(char[][] grid, char[][] nextgrid) {
        for (int i = 0; i < numOfRow + 1; i++) {
            for (int j = 0; j < numOfCol + 1; j++) {
                grid[i][j] = nextgrid[i][j];
            }
        }
    }

    public static boolean isEmpty(char[][] grid, char[][] nextgrid) {
        boolean blankFlag = true;
        for (int i = 0; i < numOfRow + 1; i++) {
            for (int j = 0; j < numOfCol + 1; j++) {
                if (grid[i][j] != DOT) {
                    blankFlag = false;
                }
            }
        }
        return blankFlag;
    }

    public static boolean isSame(char[][] grid, char[][] nextgrid) {
        boolean sameFlag = false;
        for (int i = 0; i < numOfRow + 1; i++) {
            for (int j = 0; j < numOfCol + 1; j++) {
                if (grid[i][j] == nextgrid[i][j]) {
                    sameFlag = true;
                    break;
                }
            }
        }
        return sameFlag;
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);// erases panel Contents
        g.setColor(Color.black);
        if (sameFlag == false && blankFlag == false) {
            print(g);// or whatever method you use to print the world
        } else {
            if (sameFlag == true) {
                g.drawString("The worlds are repeating!", 10, 250);
            }
            if (blankFlag == true) {
                g.drawString("The world is blank!", 10, 250);
            }
        }
    }

}

所以我正在为我的CS121 java类中的作业编写这个编码,这是Conway的生命游戏。代码背后的想法是用户键入文件名(文本文件),其具有句点和x的网格。期间是死细胞,x是活细胞。

当我尝试输入文件名时,它会自动将我引发到异常并说Please insert a valid file name.

如果我再次尝试输入文件名,则会出现此错误。

Exception in thread "Thread-2" java.lang.StringIndexOutOfBoundsException: String index out of range: 9
    at java.lang.String.charAt(Unknown Source)
    at edu.bsu.cs121.mamurphy.Test.read(GameOfLifeMain.java:103)
    at edu.bsu.cs121.mamurphy.Test.begin(GameOfLifeMain.java:79)
    at edu.bsu.cs121.mamurphy.Temp.run(GameOfLifeMain.java:46)

所以我得到的一个答案是,我需要提示我在一个循环中询问用户文件名,只有在文件名成功打开时才会发生。我怎么能这样做呢?

1 个答案:

答案 0 :(得分:0)

在捕获尝试打开文件的异常后,您的代码将继续进入循环以从文件中读取(未打开)。由于扫描程序(世界)仍然分配给System.in,因此下次尝试输入文件名将作为文件中的第一行处理。由于您输入的名称短于预期的列数,因此在's.charAt(numOfCol - 1)'

处获得超出范围异常的索引

您需要在循环中提示您输入文件名,该循环仅在文件成功打开时退出。