Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Simple if-else not working for me.

  1. #1
    Member
    Join Date
    Feb 2013
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Simple if-else not working for me.

    Hi all. Me again. I hope everyone is doing OK. I have a simple enough little program here, that according to my n00b mind, should be working just fine. 'Cept I keep getting this error message: "java:21: error: variable hasInsurance might not have been initialized" and I don't really know why. If anyone cares to chime in, I'd be grateful.


    import javax.swing.JOptionPane;
     
    public class MyMedicalProgram2
    {
    	public static void main(String[] args)
    	{
    		String isPatient;
    		String hasInsurance;
    		String isCurrent;
     
    		isPatient = JOptionPane.showInputDialog("Are you a current patient? Enter Yes or No.");
     
    		if (isPatient != "Yes")
    			{
    				JOptionPane.showMessageDialog(null, "Please fill out our new-patient forms. All of them.");
    			}
    		else
    			{
    				hasInsurance = JOptionPane.showInputDialog("Do you have insurance? Enter Yes or No");
    			}
    		if (hasInsurance != "Yes")
    			{
    				JOptionPane.showMessageDialog(null, "Well then we're going to need the money up front.");
    			}
    		else
    			{
    				isCurrent = JOptionPane.showInputDialog("Is your information current? Enter Yes or No.");
    			}
    		if (isCurrent != "Yes")
    			{
    				JOptionPane.showMessageDialog(null, "Please fill out these forms. All of them.");
    			}
    		else
    			{
    				JOptionPane.showMessageDialog(null, "Please have a seat and we will be with you in our own good time.");
    			}
     
    	}
     
    }


  2. #2
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default Re: Simple if-else not working for me.

    Initialize your variables to a value before the if-else control statement to make the compiler happy.

  3. The Following User Says Thank You to Zyrion For This Useful Post:

    Praetorian (March 5th, 2013)

  4. #3
    Member
    Join Date
    Feb 2013
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Simple if-else not working for me.

    Thanks Zyrion. You're pretty quick to respond. So, ok, I went back and initialized the variables. Bear in mind, I'm still in n00b-think, so when I got that error message, I thought it was mistaken because the variables are strings. It still doesn't make complete sense to me, the whole 'why' of it, but I've come to see that it's just something that has to be done. I know that if a variable is not initialized, it still has an arbitrary value of sorts, so I guess it doesn't hurt to assign it a precise value to start with. Is that the whole 'why' of it? I've sort of retooled the code, has only one slight issue remaining. It iterates beyond what it is supposed to do when a "no' answer is entered.. Any thoughts would be appreciated.
    import javax.swing.JOptionPane;
     
    public class MyMedicalProgram2
    {
    	public static void main(String[] args)
    	{
    		String isPatient = "I'm";
    		String hasInsurance = "not";
    		String isCurrent = "happy";		
    		isPatient = JOptionPane.showInputDialog("Are you a current patient? Enter Yes or No.");
     
    		if (!isPatient.equalsIgnoreCase("Yes"))
    			{
    				JOptionPane.showMessageDialog(null, "Please fill out our new-patient forms. All of them.");
    			}
    		else
    			{
    				hasInsurance = JOptionPane.showInputDialog("Do you have insurance? Enter Yes or No");
    			}
    		if (!hasInsurance.equalsIgnoreCase("Yes"))
    			{
    				JOptionPane.showMessageDialog(null, "Well then we're going to need the money up front.");
    			}
    		else
    			{
    				isCurrent = JOptionPane.showInputDialog("Is your information current? Enter Yes or No.");
    			}
    		if (!isCurrent.equalsIgnoreCase("Yes"))
    			{
    				JOptionPane.showMessageDialog(null, "Please fill out these forms. All of them.");
    			}
    		else
    			{
    				JOptionPane.showMessageDialog(null, "Please have a seat and we will be with you in our own good time.");
    			}
     
    	}
     
    }


    --- Update ---

    Nevermind. Nothing a little 'System.exit(0)' couldn't fix. Don't know if it's 'pretty' or conventional, but it works.

    How do I close this out as 'Solved'?

  5. #4
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default Re: Simple if-else not working for me.

    Under thread tools above your top post use the drop-down box and select 'Solved'.

Similar Threads

  1. Simple Calcular Program, But not working!!!
    By achugr in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 1st, 2012, 02:53 PM
  2. Simple Division not working heeeeelp
    By ashboi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 9th, 2012, 05:48 PM
  3. Simple code, cant get it working.
    By Malmen in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 8th, 2011, 02:28 PM
  4. Replies: 4
    Last Post: August 10th, 2011, 11:16 AM
  5. Need Help Getting A Simple Stop-Watch Working?
    By logmein in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 2nd, 2011, 07:57 AM