Search code examples
javabuilding

Arrary list while creating a buildin


package uk.ac.reading.Nischal.buidlingconsole;

import java.util.ArrayList;

public class Building {


    private int xSize = 10;             // size of building in x
    private int ySize = 10;             // and y
    private static String test;
    StringSplitter bigBox;
    private static  ArrayList<Room> allRooms;       // array of rooms


    public Building(String build) {
        setBuilding(build);
    }

    public void setBuilding(String bS) {

        // allRooms.clear();
        allRooms= new ArrayList<Room>();


        bigBox = new StringSplitter(bS, ";");
        String first = (bigBox.getNth(0, ""));
        StringSplitter sizeXY = new StringSplitter(first, " ");
        xSize = Integer.parseInt(sizeXY.getNth(0, ""));
        ySize = Integer.parseInt(sizeXY.getNth(1, ""));
        //test = bigBox.getNth(3, "");

        for(int i = 0; i < bigBox.numElement(); i++){
            String r = (bigBox.getNth(i, ""));
            allRooms.add(new Room(r));
        }

    }

    public String toString(){
        String res = "Building size " + xSize + "," + ySize + "\n" ;
        String s = "";
        for (Room r : allRooms) 
            s = s + r.toString() + "\n";



        return res + s;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Building b = new Building("11 11;0 0 5 5 3 5;6 0 10 10 6 6;0 5 5 10 2 5");  // create 
        System.out.println(b.toString());
        System.out.println(test);

    }
}

i'm trying to create a building by passing a string of the form, 11 11;0 0 5 5 3 5;6 0 10 10 6 6;0 5 5 10 2 5 but i'm getting unknown errors

i'm told I have to use array list and here I've tried to do so yet I get weird errors.


Solution

  • I hope i'm getting this right, that this StringSplitter does nearly the same like the split-method from String.

    Due the condition, that the 0th-Element of the bigBox, contains the xSize and ySize, i think your loop should start with 1 and not with 0.

        for(int i = 1; i < bigBox.numElement(); i++){
            String r = (bigBox.getNth(i, ""));
            allRooms.add(new Room(r));
        }
    

    It would be helpful if you can show as the error-logs.