【问题标题】:File to 2d array printing null (Java)文件到二维数组打印空(Java)
【发布时间】:2015-01-21 19:17:06
【问题描述】:

我目前正在开发一款游戏,并且现在(在游戏中)是一名关卡编辑器/阅读器。我这样做的方法是将级别存储在 txt 文件中,然后将 txt 文件输入缓冲区读取器,然后将文本文件放入像这样 [lines][words] 的二维数组中。我的意思是我希望第一个方括号中的数字代表您想要的行(来自文件),然后从该行中选择一个带有另一个方括号的单词。

我可以打印文件并将其存储在一个类中的二维数组中,但是当我尝试将信息传输到另一个类时:String strLevel = TextHandler.getLevel("res/levels.txt")
然后当我尝试打印 strLevel[1][1] 它只是在控制台中打印出 null(s)...

这是 TextHandler.java 的代码:

公共类 TextHandler {

final static int LEVELS = Level.LEVEL_AMOUNT;
final static int MAX_LINES = 100;
final static int MAX_WORDS = 100;



public static String[][] getLevel(String path) throws IOException {
    int x = 0;
    int y = 0;

    String lines[][] = new String[MAX_LINES][MAX_WORDS];


    BufferedReader in = new BufferedReader(new FileReader(path));

    String line;

    while ((line = in.readLine()) != null)  //file reading
    {
       String[] values = line.split(" ");

       for (String str : values){   
           lines[x][y] = str;
           System.out.print(lines[x][y] + " ");    //This print funciton works!

           y += 1; 
       }

       x += 1;
       System.out.println("");
    }


    return lines;
}

}

这是文本文件:

0级
添加 TYPE_WALL 100 300 400 100
添加 TYPE_WALL 200 300 200 50
添加 TYPE_WALL 400 53 800 10
添加 TYPE_PLAYER 200 40

1级
添加 TYPE_WALL 100 300 400 100
添加 TYPE_WALL 200 300 200 50
添加 TYPE_WALL 400 53 800 10
添加 TYPE_PLAYER 100 200

2级
//空层

3级
//空层

4级
//空层

5级
//空层

6级
//空层

这是结果:

0级
添加 TYPE_WALL 100 300 400 100
添加 TYPE_WALL 200 300 200 50
添加 TYPE_WALL 400 53 800 10
添加 TYPE_PLAYER 200 40

1 级 添加 TYPE_WALL 100 300 400 100 添加 TYPE_WALL 200 300 200 50 添加 TYPE_WALL 400 53 800 10 添加 TYPE_PLAYER 100 200

二级

3级

4级

5级

6级









正如我之前所说,问题是当我尝试创建一个等于 getLevel('path') 的二维数组然后打印它时,它会打印“null”!!

这是一些Level类的代码:

包 com.sontvedt.engine;

导入 java.io.IOException; 导入 java.util.ArrayList;

导入 com.sontvedt.entity.*;

公开课级别{

Utilities util = new Utilities();
TextHandler txt = new TextHandler();

static final int LEVEL_AMOUNT = 10;

String strLevel[][];

Player player;
Wall wall = new Wall(42,42,42,42);

ArrayList<Entity> objects = new ArrayList<Entity>();
Enemy[] enemies = new Enemy[20];        //This it later going to be added in the entities arrayList. The reason its a normal array is so that it would work with collision detection



//Pre-init of the levelAmount should be done here
public Level(){
    player = new Player(42, 42);

    try {
        strLevel = TextHandler.getLevel("res/levels.txt");
    } catch (IOException e) {
        e.printStackTrace();
    }



    //Init entities ArrayList       
        createLevels(0);

}

//Collision check
public void update(){
}


/*  LEVEL CREATION
 * 
 * The first thing that is going to be in the "level editor" text file
 * is which level the items are going to be added to. Like so:
 *      - level 0
 * 
 * The entity adding is later going to be added in a text file as so:
 *      - Group entity xPos yPos Width Height
 *      - enemies enemy 60 60, 200, 100
 * 
 * The player postition is the first two numbers in txt file,
 * Player is also the last item that should be added to entities
 * 
 * This may have to be in the end of the superloop
 *          
 * for(int i = 0; i < enemies.length; i++)
 *      entities[level].add(enemies[i]);
 * 
 */

public void createLevels(int level){
    objects.add(player);
    objects.add(wall);          

    //Should print add from text file/TextHandler.getLevel()
    System.out.println(strLevel[1][1]);


    for(int i = 0; i >= TextHandler.MAX_LINES; i++){
        for(int j = 0; j >= TextHandler.MAX_WORDS; j++){

            if(strLevel[j][j] == "level"){
                System.out.println("I found the word 'level', I'm so cool, look at meee... :P");
            }
        }
    }   
}

一直在寻找答案,但我一无所获......请帮助!

【问题讨论】:

  • 永远不要返回全局变量!将行本地化并返回。
  • 当您说 getLevel 打印 null 时,您的意思是 system.out.println(getLevel(pathvar)) 打印 null 吗?或者你遍历数组并且每个值都是空的
  • 已修复!但没有帮助
  • 是的,这不是问题,它只是困扰着我:p 我还在寻找,但文本文件的示例可能有用
  • 当我尝试在另一个类中设置一个名为 strLevel 的二维数组等于 TextHandler.getLevel() 然后尝试打印 (strLevel[1][1]) 我在底部得到结果只是说了几次 null

标签: java printing null multidimensional-array


【解决方案1】:

当您到达新行时,您不会重置您的 y 值:

while ((line = in.readLine()) != null)  //file reading
{
   String[] values = line.split(" ");
   y=0; // gotta start back at 0 when we get to a new line

其他东西

//Should print add from text file/TextHandler.getLevel()
System.out.println(strLevel[1][1]);

注释不正确,应该打印TYPE_WALL。


if(strLevel[j][j] == "level"){

使用.equals 而非== 比较字符串,以进行空安全比较:

if("level".equals(strLevel[j][j]))

if(strLevel[j][j] == "level"){

我很确定你的意思是strLevel[i][j],也因为你在其他地方使用xy,你应该尽量保持一致。也许将 i 重命名为 xj 重命名为 y

【讨论】:

  • 顺便说一句,它们在 "level".equals(strLevel[j][j]) 和 strLevel[j][j].equals("level") 之间有什么区别吗??
  • 如果strLevel[j][j] 为空,您将获得NullPointerException,否则它们完全相同。有关它的更多信息,请参阅此问题stackoverflow.com/questions/3321526/…。在您的情况下,由于字符串数组中的空值,这很重要。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-02
相关资源
最近更新 更多