Reputation: 147
I apologize in advance if the information is lacking, this is new to me!
So the assignments says; there is a Hotel for pets (dogs, cats and snakes) and the program is supposed to print out how much food and what type of food they are supposed to eat. User writes the name of a pet and it should print which it does.
What I don't understand is how to write a conditional statement that says if NONE of the element's name match input, write "We have no pets with the name (input) at our hotel".
Reason why I got this problem is because I can't reach any elements unless I create a foreach-loop and I don't want a message to pop up for every element just after loop ends.
import javax.swing.*;
public class Main {
public static void main(String[] args) {
Dog d1 = null;
Dog d2 = null;
Cat c1 = null;
Cat c2 = null;
Snake s1 = null;
try {
d1 = new Dog("Sixten", 5);
d2 = new Dog("Dogge", 10);
c1 = new Cat("Venus", 5);
c2 = new Cat("Ove", 3);
s1 = new Snake("Hypno", 1);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
System.exit(0);
}
Hotel h1 = new Hotel();
h1.addPet(d1);
h1.addPet(d2);
h1.addPet(c1);
h1.addPet(c2);
h1.addPet(s1);
h1.getPets(); // gets the list with all the pets
while (true) {
String input = JOptionPane.showInputDialog("What pet(name of pet) needs feeding?");
if (input == null){
System.exit(0);
break;
}
else if(input.equals("")){
JOptionPane.showMessageDialog(null, "Invalid input!");
}// HERE IS WHERE I WANT THE STATEMENT
else if(**Statement that says if input isn't equal to any of the animal's name**){
}
else{
input = input.toLowerCase();
for(Pet pet: h1.getPets()){
String text1 = String.format("%s%10s%10s\n", "Namn:", "Mått:", "Sort:");
String text2 = String.format("%s%10.2f%16s", pet.getName(), pet.measureFood(), pet.getFoodName());
String text3 = "---------------------------------------\n";
text1 = text1 + text3 + text2;
if (pet.getName().toLowerCase().equals(input)) {
JOptionPane.showMessageDialog(null,text1);
break;
}
}
}
}
}
}
Upvotes: 0
Views: 483
Reputation: 272865
You don't need an extra condition there, in fact.
If you wrote a separate condition to check if none of the pets' names match the input, then you would have iterated over the pets list twice, which is redundant.
Notice that if a pet is found, the if
inside the for
will be run. We can set a boolean
variable to true in the if
, and check it after the loop to see if a pet is found:
// in the else branch of the outermost if
boolean petFound = false; // note this line
input = input.toLowerCase();
for(Pet pet: h1.getPets()){
String text1 = String.format("%s%10s%10s\n", "Namn:", "Mått:", "Sort:");
String text2 = String.format("%s%10.2f%16s", pet.getName(), pet.measureFood(), pet.getFoodName());
String text3 = "---------------------------------------\n";
text1 = text1 + text3 + text2;
if (pet.getName().toLowerCase().equals(input)) {
JOptionPane.showMessageDialog(null,text1);
petFound = true; // note this line
break;
}
}
if (!petFound) {
// show the message that there is no pet with the input name
}
Upvotes: 1
Reputation: 542
You could use a flag, "petFound", and set it to true in the for loop if pet has been found. Check after the loop for flag value, and print the not found message if the flag is false.
If you are looking into Java8, replace the loop
Optional<Pet> pet = h1.getPets().stream().filter(pet.getName().toLowerCase().equals(input)).findFirst();
if(pet.isPresent()){
pet.get();// gives the pet
}
else{
// Print pet not found
}
Upvotes: 1