kiako_09
06-07-2008, 09:19 PM
I need some help with a while loop. I'm writing a program that determines the due date of a pregnant animal based on their date of conception and gestation period (kind of strange, I know). I need to request the user to enter a letter code to into a dialog box, and then use that code to determine the type of animal. If the code doesn't match one of the animals (i.e. they enter a letter that isn't valid), the program displays a message stating it's invalid and prompts the user for a different letter code. As of now, the program prompts the user for the letter code, but even if they enter a valid one, the program determines that it's invalid and loops over and over... Can someone please point out what I've done wrong? Thanks!
All variables have been declared/initialized previously
-----------------------------------------------------------------------
boolean token = false;
while (token == false){
userCode = JOptionPane.showInputDialog(null, "Enter the code for the type of animal", "Animal type", JOptionPane.QUESTION_MESSAGE);
if ((userCode == "h")||(userCode == "H")){
animalType = "horse";
gestationPeriod = 336;
token = true;
}
else if ((userCode == "c")||(userCode == "C")){
animalType = "cow";
gestationPeriod = 280;
token = true;
}
else if ((userCode == "p")||(userCode == "P")){
animalType = "pig";
gestationPeriod = 115;
token = true;
}
else if ((userCode == "d")||(userCode == "D")){
animalType = "dog";
gestationPeriod = 63;
token = true;
}
else if ((userCode == "k")||(userCode == "K")){
animalType = "cat";
gestationPeriod = 63;
token = true;
}
else if ((userCode == "a")||(userCode == "A")){
animalType = "hamster";
gestationPeriod = 16;
token = true;
}
else if ((userCode == "r")||(userCode == "R")){
animalType = "rabbit";
gestationPeriod = 31;
token = true;
}
else if ((userCode == "g")||(userCode == "G")){
animalType = "guinea pig";
gestationPeriod = 68;
token = true;
}
else
{
System.out.println("Invalid animal type; Try again");
}
}
All variables have been declared/initialized previously
-----------------------------------------------------------------------
boolean token = false;
while (token == false){
userCode = JOptionPane.showInputDialog(null, "Enter the code for the type of animal", "Animal type", JOptionPane.QUESTION_MESSAGE);
if ((userCode == "h")||(userCode == "H")){
animalType = "horse";
gestationPeriod = 336;
token = true;
}
else if ((userCode == "c")||(userCode == "C")){
animalType = "cow";
gestationPeriod = 280;
token = true;
}
else if ((userCode == "p")||(userCode == "P")){
animalType = "pig";
gestationPeriod = 115;
token = true;
}
else if ((userCode == "d")||(userCode == "D")){
animalType = "dog";
gestationPeriod = 63;
token = true;
}
else if ((userCode == "k")||(userCode == "K")){
animalType = "cat";
gestationPeriod = 63;
token = true;
}
else if ((userCode == "a")||(userCode == "A")){
animalType = "hamster";
gestationPeriod = 16;
token = true;
}
else if ((userCode == "r")||(userCode == "R")){
animalType = "rabbit";
gestationPeriod = 31;
token = true;
}
else if ((userCode == "g")||(userCode == "G")){
animalType = "guinea pig";
gestationPeriod = 68;
token = true;
}
else
{
System.out.println("Invalid animal type; Try again");
}
}