if statement or method not working

DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: if statement or method not working

  1. #1
    Join Date
    Nov 2005

    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


    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
    what number would you like to convert it from?
    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

  2. #2
    Join Date
    Dec 2005
    New Jersey
    Next time you post code, instead of highlighting it blue, embed it in the [code][/code] 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))

  3. #3
    Join Date
    Nov 2005
    thanks a lot for your help

Similar Threads

  1. jdi popframes method
    By caid in forum Java
    Replies: 0
    Last Post: 09-01-2005, 01:12 AM
  2. Replies: 2
    Last Post: 07-19-2005, 06:09 AM
  3. recordset.Find method
    By Alan Shiers in forum VB Classic
    Replies: 3
    Last Post: 12-27-2000, 04:22 PM
  4. Replies: 4
    Last Post: 10-17-2000, 11:25 AM
  5. VSS:sharing same working folder without multiple checkouts
    By rahul phatak rahulphatak in forum ASP.NET
    Replies: 0
    Last Post: 06-01-2000, 05:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center