Conditional branching problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tiktik
    New Member
    • Nov 2008
    • 14

    #1

    Conditional branching problem

    Hi all...

    I am compiling this simple program which determines the ticket price for the person, depending on the criteria entered.

    My problem is, however, that the answer at the end is always "FREE" (as if the program is not considering the other if branches =(

    Code:
    System.out.println("Enter your age ");
        	    	int age = Keyboard.readInt();
        	    	
        	    	System.out.println("Enter your locality (Maltese/Gozitan) ");
        	    	String loc = Keyboard.readString();
        	    	
        	    	String price ;
        	    		
        	    	if ((age < 15 ) && (loc == "Gozitan")) price = "EUR 0.3" ;
        	    	  else if ((age < 15 ) && (loc == "Maltese")) price = "EUR O.5" ; 
        	    	      else if ((( age > 14) && (age < 60 )) && loc == "Gozitan" ) price = "EUR 1";
        	    	           else if ((( age > 14) && (age < 60 )) && loc == "Maltese" ) price = "EUR 2";
        	    	                else price = "FREE" ;
        	    	           
        	       System.out.println ("Your ferry ticket costs " +price);
    Any idea of what might be wrong with this program pls?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Don't compare Strings for equality by using the '==' operators. Read the API documentation for the String class and use the equals() method.

    kind regards,

    Jos

    Comment

    • tiktik
      New Member
      • Nov 2008
      • 14

      #3
      problem solved =)

      thanks

      Comment

      Working...