Hello,
I am having problems with a method I’m working on to handle adding and removing objects from two array lists.
public class Player {
public static ArrayList<String> inventory = new ArrayList<String>();
UserInput input = new UserInput();
public void start() {
if (UserInput.getInput("").equalsIgnoreCase("take book")) {
if (Room1.room1.contains("book")) {
inventory.add("book");
Room1.room1.remove("book");
getStatus();
Room1.getStatus();
}
} else if (UserInput.getInput("").equalsIgnoreCase("drop book")) {
if (inventory.contains("book")) {
inventory.remove("book");
Room1.room1.add("book");
getStatus();
Room1.getStatus();
}
} else if (UserInput.getInput("").equalsIgnoreCase("take paper")) {
if (Room1.room1.contains("paper")) {
inventory.add("paper");
Room1.room1.remove("paper");
getStatus();
Room1.getStatus();
}
} else if (UserInput.getInput("").equalsIgnoreCase("drop paper")) {
if (inventory.contains("paper")) {
inventory.remove("paper");
Room1.room1.add("paper");
getStatus();
Room1.getStatus();
}
}
reStart();
}
It (sorta) does what I want it to do…
But for for some reason I have to type for example “take book” like four times before it actually prints the outcome.
And for each input it seems to be random as to how many times I have to type it in before it actually prints it.
Just to clarify so you can help me better…
the reStart() method just calls the start() method again.
The getStatus() methods from both the Room1 class and Player class just print their equivalent Array list.
This is the GetInput() method…
public static String getInput(String prompt) {
BufferedReader stdin = new BufferedReader(
new InputStreamReader(System.in));
System.out.print(prompt);
System.out.flush();
try {
return stdin.readLine();
} catch (Exception e) {
return "Error; " + e.getMessage();
}
}
If there is more info you need to help, let me know…
Also, If you have any suggestion on a better idea on how I should do this PLEASE let me know
I am very new to java coding and I am learning, this is a text adventure I am working on…
Thank you!