0

I am currently working on a game and right now (in the game) a level-editor/reader. The way I am doing this is by storing the levels in a txt file and then feeding the txt file into a bufferreader and then putting the text file into a 2d array like this [lines][words]. By this I mean that I want the number in the first square bracket to represent what line you want (from the file), and then choosing a word from that line with the other square bracket.

I get to print the file and store it in a 2d array in one class, but when i try to transfer the information to another class like so: String strLevel = TextHandler.getLevel("res/levels.txt")
And when i then try to print strLevel[1][1] it just print out null(s) in the console...

Here is the code for 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;
}

}

This is the text file:

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

This is the outcome:

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

As i said earlier, the problem is when i try to make a 2d array that is equals to getLevel('path') and then print it, it prints "null"!!

Here is some Level class code:

package com.sontvedt.engine;

import java.io.IOException; import java.util.ArrayList;

import com.sontvedt.entity.*;

public class Level {

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");
            }
        }
    }   
}

Have been searching for the answer for quite some time but i found nothing... Pleace Help!

Devtons
  • 15
  • 8
  • don't return global variables ever! Make lines local and return it. – ghostbust555 Jan 21 '15 at 19:21
  • when you say getLevel prints null do you mean that system.out.println(getLevel(pathvar)) prints null? or that you ran through the array and every value was null – ghostbust555 Jan 21 '15 at 19:24
  • fixed it! but didn't help – Devtons Jan 21 '15 at 19:24
  • yes wasn't the problem it was just bugging me :p I'm still looking but a sample of the text file might be useful – ghostbust555 Jan 21 '15 at 19:25
  • When i try to set a 2D array called strLevel in another class equals to TextHandler.getLevel() and then tried to print (strLevel[1][1]) i get the outcome at the bottom that just says null a couple of times – Devtons Jan 21 '15 at 19:26
  • The sample of the textFile is the first blockquote – Devtons Jan 21 '15 at 19:26
  • As you may or may not have seen i print out the outcome of lines in TextHandler.getLevel() function – Devtons Jan 21 '15 at 19:27
  • Hm. I really don't see any problems (except that oversized text files will walk off the array if your not careful) which leads me to believe your problem is in the other class you mentioned. Try taking just this function and calling it directly and then looping through it printing the results. When in doubt simplify the problem – ghostbust555 Jan 21 '15 at 19:29
  • I didn't mention this earlier but instead of doing this the general solution is to serialize and deserialize the array directly (into binar, json, bosn, xml etc) which is all done automagically by java serialization libraries – ghostbust555 Jan 21 '15 at 19:34
  • I already am aware of this, but thanks anyways.. The problem is that I am not allowed to do this because my teacher saw what i was doing with this and asked me to try to figure out a way to use this system. Another reason is that my non-programmer friends find this the easiest way. – Devtons Jan 21 '15 at 19:37

1 Answers1

1

Your not resetting your y value when you reach a new row:

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

Other stuff

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

The comment is incorrect, it should print TYPE_WALL.


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

compare Strings using .equals not ==, for a null safe comparison use:

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

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

im pretty sure you mean strLevel[i][j], also because you use x and y in other places you should try to stay consistent. Maybe rename i to x and j to y.

ug_
  • 11,267
  • 2
  • 35
  • 52
  • btw, is their any difference between "level".equals(strLevel[j][j]) and strLevel[j][j].equals("level")?? – Devtons Jan 21 '15 at 19:48
  • If `strLevel[j][j]` is null you will get a `NullPointerException` otherwise they are exactly the same. See this question for more information about it http://stackoverflow.com/questions/3321526/should-i-use-string-isempty-or-equalsstring . In your case it matters a whole lot because of the null values in your string array. – ug_ Jan 21 '15 at 19:50