The issue I’m having is that the for
loop with the comment never executes for any input I provide. This is a method that depends on no output being passed to or back from it from a main menu.
There is a method being called that initilizes dogList with values.
public static void main(String[] args) {
Scanner scan = new Scanner(System.in); //scanner object
addMonkeySpecies(); //add species to monkey species list for later validation
//while loop that persists until users enter Q regardless of case
while (!command.equalsIgnoreCase("q")) {
//display menu options
displayMenu();
command = scan.nextLine(); //get user command
//if-else tree for user selections
//terminate program selection
if (command.equalsIgnoreCase("Q")) {
System.out.println("nExiting program . . . Goodbye!");
break; //breaks loop, terminates program
}
//intake a new dog selection
else if (command.equalsIgnoreCase("1")) {
intakeNewDog(scan);
}
//intake a new monkey selection
else if (command.equals("2")) {
intakeNewMonkey(scan);
}
//reserve a animal selection
else if (command.equals("3") ) {
reserveAnimal(scan);
}
//print all available dogs
else if (command.equals("4")) {
printAnimals("dog");
}
//print all available monkeys
else if (command.equals("5")) {
printAnimals("monkey");
}
//print all non reserved animals in service option
else if (command.equals("6")) {
printAnimals("available");
}
//all other invalid input
else {
System.out.println("nPlease enter a valid selection.");
}
}
}
public static void initilizeDogList() {
Dog dog1 = new Dog("Spot", "German Shepherd", "male", "1", "25.6", "05-12-2019", "United States", "in service", false, "United States");
Dog dog2 = new Dog("Rex", "Great Dane", "male", "3", "35.2", "02-03-2020", "United States", "Phase I", false, "United States");
Dog dog3 = new Dog("Bella", "Chihuahua", "female", "4", "25.6", "12-12-2019", "Canada", "in service", true, "Canada");
dogList.add(dog1);
dogList.add(dog2);
dogList.add(dog3);
}
public static void reserveAnimal(Scanner scan) {
System.out.println("Enter animal type to reserve [Dog / Monkey]:");
String animalType = scan.nextLine();
if (animalType.equalsIgnoreCase("dog")) {
System.out.println("Enter dog's name to change reserve status: ");
String dogName = scan.nextLine();
System.out.println("if dog");
for (Dog dog : dogList) { //this is the loop that never executes
System.out.println("for loop");
if (dog.getName().equalsIgnoreCase(dogName)) {
System.out.println(dog.getName() + " is currently reserved: " + dog.getReserved());
System.out.println("Enter new reserved status [Yes / No]: ");
String reserveInput = scan.nextLine();
boolean reserved;
while (!reserveInput.equalsIgnoreCase("yes") && !reserveInput.equalsIgnoreCase("no")) {
if (reserveInput.equalsIgnoreCase("yes")) {
reserved = true;
} else if (reserveInput.equalsIgnoreCase("no")) {
reserved = false;
} else {
System.out.println("Enter a valid reserve status - only [Yes / No] accepted.");
}
}
}
}
}
RESULTS:
Rescue Animal System Menu
[1] Intake a new dog
[2] Intake a new monkey
[3] Reserve an animal
[4] Print a list of all dogs
[5] Print a list of all monkeys
[6] Print a list of all animals that are not reserved
[q] Quit application
Enter a menu selection:
3
Enter animal type to reserve [Dog / Monkey]:
dog
Enter dog's name to change reserve status:
bella
if dog
//here it returns to the main menu as if it never kicks the for loop in action it doesnt even display the print line entered for testing
Rescue Animal System Menu
[1] Intake a new dog
[2] Intake a new monkey
[3] Reserve an animal
[4] Print a list of all dogs
[5] Print a list of all monkeys
[6] Print a list of all animals that are not reserved
[q] Quit application
Enter a menu selection:
7
@JustAnotherDeveloper called it. method to initialize lists was never called resulting in the for loop not having anything to iterate over in the array list.
8