Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

if statement or method not working

i am having a prolblem with an if statement not responding to a string when it should.

hereis my code:

import java.lang.String;
public class timeconverter {
public static void main(String[]args) {
// choose
String choice = null;
double num = 0;
EasyReader console = new EasyReader();

System.out.println("Please enter witch one you want to convert of the following.");
System.out.println("\n hours-minites, days-hours, minites-hours, hours-days");
choice = console.readWord();
System.out.println("what number would you like to convert it from?");


num = console.readDouble();// the number of units to convert to anther unit

choice.toLowerCase();

if (choice == "hours-minites")
{ hoursToMinites(num); }
else if (choice == "days-hours")
{ daysToHours(num); }
else if (choice == "minites-hours")
{ minitesToHours(num); }
else if (choice == "hours-days")
{ hoursTodays(num); }
else {
System.out.println("no match " + choice); }//debug
}
public static void hoursToMinites(double num) {
System.out.println( num + " hours = " + (num * 60)+ " minites");
}//hoursToMinites end

public static void daysToHours(double num) {
System.out.println( num + " days = " + (num / 24)+ " hours");
}//daysToHours end

public static void minitesToHours(double num) {
System.out.println( num + " minites = " + (num / 60)+ " hours");
}// minitesToHours end

public static void hoursTodays(double num) {
System.out.println( num + " hours = " + (num * 24)+ " days");
}//hoursToDays end

}//class end

here is my output:

Please enter witch one you want to convert of the following.

hours-minites, days-hours, minites-hours, hours-days
days-hours
what number would you like to convert it from?
2
no match days-hours

as you can see the string choice is assigned the value "days-hours" and it should trigger the if if statement to run daysToHours but it does not. Also i put in a else to show the vale if it does not find anything that equals it and it is the (same thing as the if statement) but the if statement will not work or the method maybe. It would be great if someone could show me what im doing wrong. thanks
[2417 byte] By [Darrel104] at [2007-11-11 7:52:28]
«« VB Gameing
»» Help!
# 1 Re: if statement or method not working
Next time you post code, instead of highlighting it blue, embed it in the tags. Thanks.

You are having a very common problem. When you use the == operator to compare objects, it will compare memory. You want to use the equals(Object) method. Change your if statements to look like this:
if (someString.equals(someOtherString))
Also, instead of using the toLowerCase() method, you can use the equalsIgnoreCase method:
if (someString.equalsIgnoreCase(someOtherString))
destin at 2007-11-11 22:37:09 >
# 2 Re: if statement or method not working
thanks a lot for your help
Darrel104 at 2007-11-11 22:38:03 >