Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jun 2008
    Location
    Saskatchewan, Canada
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Trouble with a while loop...

    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
    -----------------------------------------------------------------------
    Code:
    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");
    		}
    
    	}
    Last edited by Aradon; 06-08-2008 at 03:38 PM. Reason: -- format the code - Aradon

  • #2
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    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.

    Code:
    (userCode == "h")
    The address of userCode == the address of our temporary placevalue "h".

    Instead String has something called equals and equalsIgnoreCase.

    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.

    Code:
      if ((userCode.equals("h"))||(userCode.equals("H"))){
    // Or --------------------
      if ((userCode.equalsIgnoreCase("h")){
    "To iterate is human, to recurse divine." -L. Peter Deutsch

  • Users who have thanked Aradon for this post:

    kiako_09 (06-08-2008)

  • #3
    New to the CF scene
    Join Date
    Jun 2008
    Location
    Saskatchewan, Canada
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •