简体   繁体   English

需要帮助,使Java方法可以从文本文件中创建2D字符串数组

[英]Need Help Making a Java Method for Creating a 2D String Array From a Text File

I've been trying to make a function in Java that will read a text file that has "1" and 0"s like this 我一直试图在Java中创建一个函数,该函数将读取具有“ 1”和“ 0”的文本文件,如下所示

11000000001111111110
11111000000000011000
11111111000000001000
11111100000000001100
11111000000000011110
11111000000000111111
11111110000000001111
11111111110001111100
11111000000000001111
11000000000000000000
10000000000000000000
00000000001100000000
00000000011110000000
10000000011000000000
11000000000000000000

and then convert the "1"s to a certain character or image and the "0"s to another character/image. 然后将“ 1”转换为某个字符或图像,将“ 0”转换为另一个字符/图像。 I'm hoping to use this for an ASCII-graphic game I've been planning on. 我希望将其用于我一直在计划的ASCII图形游戏。 However, I keep on getting the 但是,我一直在

java.lang.ArrayIndexOutOfBoundsException: 1

error on my Eclipse console. 我的Eclipse控制台上出现错误。 Below is the code for the function. 下面是该函数的代码。

public static String[][] create2DStringMatrixFromFile(String filename) throws Exception {
    String[][] map = new String[20][15];
    for (int x = 0; x < 20 ; x++) {
        for (int y = 0; y < 15; y++) {
            map[x][y] = "0";
        }
    }

File inFile = new File(filename);
Scanner in = new Scanner(inFile);
in.close();

in = new Scanner(inFile);

int lineCount = 0;
while (in.hasNextLine()) {
  String[] currentLine = in.nextLine().trim().split("\\s+"); 
     for (int i = 0; i < 15; i++) {
        map[lineCount][i] = currentLine[i];
     }
  lineCount++;
 }                                 
 return map;

}

It would be greatly appreciated if someone could point out what I did wrong :) 如果有人可以指出我做错了,将不胜感激:)

EDIT: I took into consideration of what @andrewdleach has suggested me to do and this is my code now. 编辑:我考虑了@andrewdleach建议我做什么,这是我现在的代码。

    public static String[][] create2DStringMatrixFromFile(String filename) throws IOException {

    File inFile = new File(filename);
    BufferedReader reader = new BufferedReader(new FileReader(inFile));

    String[][] map = new String[20][15];
    //Arrays.fill(map, "0");
    //fill each row with 0
    for (String[] row : map) {
        Arrays.fill(row, "0");
    }
    int rowCount = 0;
    while (reader.ready()) {
        parseLine(reader.readLine(), rowCount, map);
        rowCount++;       

    }
    reader.close(); // might delete later
     return map;
}

private static void parseLine(final String theLine, final int theCurrentRow, String[][] map) {
     final char[] eachColumn = theLine.toCharArray();
     int curColumn = 0;
     for (char c : eachColumn) {
         map[theCurrentRow][curColumn] = new String(Character.toString(c));
         curColumn++;       
     }

 }

However now I get this error whenever I run it. 但是,现在我每次运行它都会收到此错误。

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 15
at com.trees.Map.parseLine(Map.java:62)
at com.trees.Map.create2DStringMatrixFromFile(Map.java:50)

I spent the past hour or so trying to fix it, but I couldn't figure it out. 我花了大约一个小时的时间试图修复它,但我无法弄清楚。 Any tips, please? 有什么建议吗? If I can't get this to work with Arrays, I might rewrite it with ArrayLists as someone else mentioned, which might work out better. 如果无法使它与Arrays一起使用,我可能会像其他人提到的那样用ArrayLists重写它,这可能会更好。

Well, harcoding your variables is always a bad thing. 好吧,对变量进行编码始终是一件坏事。 It doesn't allow you flexibility should the size of the expected array change. 如果期望的数组大小发生变化,它将不具有灵活性。

Your error Array Index Out of Bounds is commonly referred to as an off-by-1 error, so if you sift through your code you'll probably find a place where you're trying to access an indexed portion of the 2D array that's not actually declared. 您的错误Array Index Out of Bounds通常被称为偏离1的错误,因此,如果在代码中进行筛选,您可能会发现一个试图访问2D数组的索引部分的地方。实际宣布。

All that being said, to solve your problem you may want to consider tackling it like so: 话虽如此,为解决您的问题,您可能需要考虑解决该问题,如下所示:

public static String[][] create2DStringMatrixFromFile(String filename) {

    File inFile = new File(filename);
    BufferedReader reader = new BufferedReader(new FileReader(inFile)));

    String[][] map = new String[20][15];
    Arrays.fill(map, "0");

    int rowCount = 0;
    while (reader.ready()) {
        parseLine(reader.readLine(), rowCount);
        rowCount++;       

    }                                
     return map;
}


private void parseLine(final String theLine, final int theCurrentRow) {
     final char[] eachColumn = theLine.toCharArray();
     int curColumn = 0;
     for (char c : eachColumn) {
         map[theCurrentRow][curColumn] = new String(c);
         curColumn++;       
     }

 }

Calling split("\\\\s+") does not split the value "11000000001111111110", since the value doesn't contain any spaces. 调用split("\\\\s+")不会拆分值“ 11000000001111111110”,因为该值不包含任何空格。

This means that currentLine is an array with one element, the un-split value, hence currentLine[i] will fail for i = 1 . 这意味着currentLine是一个具有一个元素(未拆分的值)的数组,因此,对于i = 1currentLine[i]将失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM