文件到二维数组打印 null (Java)
File to 2d array printing null (Java)
我目前正在开发一款游戏,现在(在游戏中)level-editor/reader。我这样做的方法是将级别存储在 txt 文件中,然后将 txt 文件送入 bufferreader,然后将文本文件放入二维数组中,如 [lines][words]。我的意思是我希望第一个方括号中的数字代表您想要的行(来自文件),然后从该行中选择一个单词和另一个方括号。
我开始打印文件并将其存储在一个 class 的二维数组中,但是当我尝试将信息传输到另一个 class 时,如下所示:String strLevel = TextHandler.getLevel("res/levels.txt")
然后当我尝试打印 strLevel[1][1] 时,它只是在控制台中打印出 null...
这是 TextHandler.java 的代码:
public class 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;
}
}
这是文本文件:
level 0
add TYPE_WALL 100 300 400 100
add TYPE_WALL 200 300 200 50
add TYPE_WALL 400 53 800 10
add TYPE_PLAYER 200 40
level 1
add TYPE_WALL 100 300 400 100
add TYPE_WALL 200 300 200 50
add TYPE_WALL 400 53 800 10
add TYPE_PLAYER 100 200
level 2
//Empty level
level 3
//Empty level
level 4
//Empty level
level 5
//Empty level
level 6
//Empty level
这是结果:
level 0
add TYPE_WALL 100 300 400 100
add TYPE_WALL 200 300 200 50
add TYPE_WALL 400 53 800 10
add TYPE_PLAYER 200 40
level 1
add TYPE_WALL 100 300 400 100
add TYPE_WALL 200 300 200 50
add TYPE_WALL 400 53 800 10
add TYPE_PLAYER 100 200
level 2
level 3
level 4
level 5
level 6
null
null
null
null
null
null
null
null
null
null
正如我之前所说,问题是当我尝试制作一个等于 getLevel('path') 的二维数组然后打印它时,它会打印 "null"!!
这是一些级别 class 代码:
套餐com.sontvedt.engine;
进口java.io.IOException;
导入 java.util.ArrayList;
导入com.sontvedt.entity.*;
public class 等级 {
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
而非 ==
比较字符串,对于空安全比较,请使用:
if("level".equals(strLevel[j][j]))
if(strLevel[j][j] == "level"){
我很确定你的意思是 strLevel[i][j]
,也因为你在其他地方使用了 x
和 y
,你应该尽量保持一致。也许将 i
重命名为 x
,将 j
重命名为 y
。
我目前正在开发一款游戏,现在(在游戏中)level-editor/reader。我这样做的方法是将级别存储在 txt 文件中,然后将 txt 文件送入 bufferreader,然后将文本文件放入二维数组中,如 [lines][words]。我的意思是我希望第一个方括号中的数字代表您想要的行(来自文件),然后从该行中选择一个单词和另一个方括号。
我开始打印文件并将其存储在一个 class 的二维数组中,但是当我尝试将信息传输到另一个 class 时,如下所示:String strLevel = TextHandler.getLevel("res/levels.txt")
然后当我尝试打印 strLevel[1][1] 时,它只是在控制台中打印出 null...
这是 TextHandler.java 的代码:
public class 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;
}
}
这是文本文件:
level 0
add TYPE_WALL 100 300 400 100
add TYPE_WALL 200 300 200 50
add TYPE_WALL 400 53 800 10
add TYPE_PLAYER 200 40level 1
add TYPE_WALL 100 300 400 100
add TYPE_WALL 200 300 200 50
add TYPE_WALL 400 53 800 10
add TYPE_PLAYER 100 200level 2
//Empty levellevel 3
//Empty levellevel 4
//Empty levellevel 5
//Empty levellevel 6
//Empty level
这是结果:
level 0
add TYPE_WALL 100 300 400 100
add TYPE_WALL 200 300 200 50
add TYPE_WALL 400 53 800 10
add TYPE_PLAYER 200 40level 1 add TYPE_WALL 100 300 400 100 add TYPE_WALL 200 300 200 50 add TYPE_WALL 400 53 800 10 add TYPE_PLAYER 100 200
level 2
level 3
level 4
level 5
level 6
null
null
null
null
null
null
null
null
null
null
正如我之前所说,问题是当我尝试制作一个等于 getLevel('path') 的二维数组然后打印它时,它会打印 "null"!!
这是一些级别 class 代码:
套餐com.sontvedt.engine;
进口java.io.IOException; 导入 java.util.ArrayList;
导入com.sontvedt.entity.*;
public class 等级 {
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
而非 ==
比较字符串,对于空安全比较,请使用:
if("level".equals(strLevel[j][j]))
if(strLevel[j][j] == "level"){
我很确定你的意思是 strLevel[i][j]
,也因为你在其他地方使用了 x
和 y
,你应该尽量保持一致。也许将 i
重命名为 x
,将 j
重命名为 y
。