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;
}
}
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire