[英]File to 2d array printing null (Java)
我目前正在开发一个游戏,现在(在游戏中)是一个关卡编辑器/阅读器。 我这样做的方法是将级别存储在txt文件中,然后将txt文件输入到bufferreader中,然后将文本文件放入2d数组中,例如[lines] [words]。 我的意思是,我希望第一个方括号中的数字表示您想要的行(来自文件),然后从该行中选择一个单词与另一个方括号。
我可以打印文件并将其存储在一个类的2d数组中,但是当我尝试将信息传输到另一个类时,例如: 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 100300400100
添加TYPE_WALL 20030020050
添加TYPE_WALL 400 53800 10
添加TYPE_PLAYER 200 401级
添加TYPE_WALL 100300400100
添加TYPE_WALL 20030020050
添加TYPE_WALL 400 53800 10
添加TYPE_PLAYER 1002002级
//空级别3级
//空级别4级
//空级别5级
//空级别6级
//空级别
结果如下:
0级
添加TYPE_WALL 100300400100
添加TYPE_WALL 20030020050
添加TYPE_WALL 400 53800 10
添加TYPE_PLAYER 200 401级添加TYPE_WALL 100300400100添加TYPE_WALL 20030020050添加TYPE_WALL 400 53800 10添加TYPE_PLAYER 100200
2级
3级
4级
5级
6级
空值
空值
空值
空值
空值
空值
空值
空值
空值
空值
正如我之前所说,问题是当我尝试制作一个等于getLevel('path')的2D数组然后打印时,它会打印“ 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");
}
}
}
}
一直在寻找答案,但是我什么也没发现。
到达新行时,您无需重置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
not ==
比较字符串,以使用安全的空比较:
if("level".equals(strLevel[j][j]))
if(strLevel[j][j] == "level"){
我非常确定您的意思是strLevel[i][j]
,也因为您在其他地方使用x
和y
时应尽量保持一致。 也许将i
重命名为x
,将j
重命名为y
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.