PDA

View Full Version : Trouble with a while loop...



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");
}

}

Aradon
06-08-2008, 02:44 PM
I need to add this to the faq I think.

Basically I assume that you're userCode is a String. As such, you can't do == comparison with Strings!

Why? Well unlike ints, doubles, floats, and booleans Strings aren't primitive types. Instead they are what we call Class types. And because every Class type is really a pointer, all you are doing with == is checking if the address of the two things are equal.



(userCode == "h")

The address of userCode == the address of our temporary placevalue "h".

Instead String has something called equals (http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#equals(java.lang.Object)) and equalsIgnoreCase (http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#equalsIgnoreCase(java.lang.String)).

Using these two methods you can see if a string is exactly equals to another string even in case, or ignoring case value.

So instead of userCode == "h" you would have.



if ((userCode.equals("h"))||(userCode.equals("H"))){
// Or --------------------
if ((userCode.equalsIgnoreCase("h")){

kiako_09
06-08-2008, 07:31 PM
Thanks! This is my first time with Java, and it's been a while for any other languages, so I'm kind out of my element at the moment. The program works perfectly now, so thanks again.