public class Map { ArrayList array = new ArrayList(); char [][] linksWorld = null; public Map(String mapFile) { try{ BufferedReader in = new BufferedReader(new FileReader(mapFile)); String data; //Read from file while ((data = in.readLine()) != null) { //Convert data to char array and add into array array.add(data.toCharArray()); } in.close(); //Creating a 2D char array using the arraylist size linksWorld = new char [array.size()][]; //Convert array from ArrayList to 2D array for (int i=0; i<array.size(); i++){ linksWorld[i] = (char[])array.get(i); } //Test 2d array for (int j=0; j<linksWorld.length; j++){ char [] temp = linksWorld[j]; for (int x=0; x<temp.length; x++) { System.out.print(temp[x]); } System.out.println(""); } } catch (Exception ex){ ex.printStackTrace(); } }//constructor //test public static void main(String[] args){ Map map = new Map("samplemap.txt"); } }

public enum TerrainType { MOUNTAIN('^', mypackage.terrain.Mountain.class), SEA('~', mypackage.terrain.Sea.class); private char type; private Class terrainClass; TerrainType(char type, Class terrainClass) { this.type = type; this.terrainClass = terrainClass; } public String getType() { return type; } public Class getTerrainClass() { return terrainClass; } }
//reads in from a text file line by line. while((s = in.readLine())!= null){ for(int cols = 0; cols<arr.length; cols++){ arr1 = s.toCharArray(); if(arr1[rows] == '^'){ //Make a new Mountain object. hyrule[rows][cols] = new Mountain(); }//if else if(arr1[rows] == '#'){ //Make a new Desert object. hyrule[rows][cols] = new Desert(); }//else if for(rows=0;rows<arr1.length;rows++){ System.out.println(hyrule[rows][cols]); } rows++; cols = arr1.length; }//for }//while
public MapSpace getMapSpace(int row, int column){ //code to retrieve specific element return MapSpaceObject; }
//reads in from a text file line by line. while((s = in.readLine())!= null){ for(int cols = 0; cols<arr.length; cols++){ arr1 = s.toCharArray(); if(arr1[rows] == '^'){ //Make a new Mountain object. hyrule[rows][cols] = new Mountain(); }//if
public class NewMap{ private int height; private int cols; private int rows; private MapSpace[][] hyrule; private int width; private String s; private char [] arr1; //Constructor public Map(){ try{ BufferedReader in = new BufferedReader(new FileReader("sampleMap.txt")); Random rdm = new Random(); while((s = in.readLine())!= null){ arr1 = s.toCharArray(); for(int cols = 0; cols<arr1.length; cols++){ for(int rows=0; rows<arr1.length; rows++){ if(arr1[rows] == '^'){ //Make a new Mountain object. hyrule[rows][cols] = new Mountain(); }//if else if(arr1[rows] == '#'){ //Make a new Desert object. hyrule[rows][cols] = new Desert(); }//else if rows++; cols = arr1.length; }//for }//for }//while in.close(); }//try catch(IOException io){ System.out.println(io); io.printStackTrace(); }//catch }//constructor
Yes, you're right, I have a text file that is about 6x8 and is full of # and ^ characters that are supposed to represent objects (each with their respective classes, Desert and Mountain).
paste the contents of your text file here so that I may view it. It looks like you are doing alot of work which is not neccessary. 6x8 is too general of a description. I need to know how many symbols per line and does one line represent the contents of 1 object.
However, when I fill the body of the getMapSpace() method and call it on my Map object, it gives me a NullPointerException. If I remember correctly, this means that my array hasn't been instantiated, right?
No, instantiated in a way means to bring an object too life by using the new keyword. You received a nullpointer exception because the element that you were trying to access at hyrule[row][col] was not present. In other words, a value was not assigned to it. I believe you performed that operation in your for loop during the read. That's why I need to see the structure of sampleMap.txt. I see you have gotten rid of the arraylist...good!!!!
then, as stated in my last post, call the getMapSpace() method on the Map object.public class NewMap{ private int height; private int cols; private int rows; private MapSpace[][] hyrule; private int width; private String s; private char [] arr1; //Constructor public Map(){ try{ BufferedReader in = new BufferedReader(new FileReader("sampleMap.txt")); Random rdm = new Random(); while((s = in.readLine())!= null){ arr1 = s.toCharArray();//don't think this is working right because arr1 is a variable that holds the bit pattern to where the char array is actually stored in memory. Also arr1 was never initialized, meaning it wasn't given a size and an index to store the the input from the text file. if 1 character equals 1 object, then in the textfile, only have 1 column of data. This will allow 1 character to be read at a time into a string variable. while the symbol is stored in the the variable, you can go straight to the if statements and compare. You wouldn't need the for loop since the file will repeat until end of file. If it turns out that you will need the char array because of its scope, you can assign the character after it passes through the if statements. for(int cols = 0; cols<arr1.length; cols++){ for(int rows=0; rows<arr1.length; rows++){ if(arr1[rows] == '^'){ //Make a new Mountain object. hyrule[rows][cols] = new Mountain(); }//if else if(arr1[rows] == '#'){ //Make a new Desert object. hyrule[rows][cols] = new Desert(); }//else if rows++; cols = arr1.length; }//for }//for }//while in.close(); }//try catch(IOException io){ System.out.println(io); io.printStackTrace(); }//catch }//constructor
| DaniWeb Message | |
| Cancel Changes | |