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);
}
}
我正在尝试通过传递以下形式的字符串来创建建筑物,
11 11;0 0 5 5 3 5;6 0 10 10 6 6;0 5 5 10 2 5
,但我遇到了未知错误
有人告诉我我必须使用数组列表,但在这里我尝试这样做,但是却出现了奇怪的错误。
答案 0 :(得分:0)
我希望我做对了,这个StringSplitter几乎和String的拆分方法一样。
由于这种情况,bigBox的第0个元素包含xSize和ySize,我认为您的循环应从1开始而不是0。
for(int i = 1; i < bigBox.numElement(); i++){
String r = (bigBox.getNth(i, ""));
allRooms.add(new Room(r));
}
如果您可以将其显示为错误日志,将会很有帮助。