java - Do not print "not found" when something is found -
i trying project , reason having issue life of me can not solve.
public static void printlist(string n){ for(int i=0; i< roomlist.size(); i++){ if(roomlist.get(i).name.equals(n)){ system.out.println("room name: " + roomlist.get(i).name + " state: " + roomlist.get(i).state); system.out.println("description: " + roomlist.get(i).desc); system.out.println("creatures in room: " + roomlist.get(i).fred()); if(roomlist.get(i).north != null){ system.out.println("north neighbor: " + roomlist.get(i).north.name); } if (roomlist.get(i).south !=null){ system.out.println("south neighbor: " + roomlist.get(i).south.name); } if (roomlist.get(i).east !=null){ system.out.println("east neighbor: " + roomlist.get(i).east.name); } if (roomlist.get(i).west !=null){ system.out.println("west neighbor: " + roomlist.get(i).west.name); } } } system.out.println("room " + n + " not exist!"); }
right if finds room object in arraylist still prints "room " + n + " not exist!" need print if room not found in arraylist
the reason happens because not found
message last statement of method. should instead return method found element , printed wanted messages.
for example assuming each room has unique name:
... if (roomlist.get(i).name.equals(n)) { ... if (roomlist.get(i).west != null) { system.out.println("west neighbor: " + roomlist.get(i).west.name); } return; } ...
Comments
Post a Comment