-3

I'm just learning Java for fun and found this forum very usefull some of the time! :)

Now, I have a problem, I can't get an application to do what I want it to do.. I just want it to run "choice" , then from my input choice I want it to run e.g ovning3 (which is "converter") but it just exits after I type in my choice "3" ..

Anything I did wrong? This is my first app with several "instances".. Is my logic is wrong?

import static javax.swing.JOptionPane.*;

public class Ovningar {


    public static void main(String[] args) {
        choice();
    }


    public static void choice(){
        String input = showInputDialog("Välj övning");
        if (input == "3")
            converter();
        if (input == "4")
            sfer();
    }

    public static void sfer() {


    }
    public static void converter() {

        String input[] = showInputDialog("Mile/Gallons?").split("/");       
        String strmiles = input[0];
        String strgallon = input[1];

        double miles = Double.parseDouble(strmiles);
        double gallon = Double.parseDouble(strgallon);

        double km = miles*1.609;
        double liter = gallon*3.785;


        showMessageDialog(null, km+"/"+liter);



    }
}
madth3
  • 7,275
  • 12
  • 50
  • 74
  • 4
    [This](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) may interest you. – Pshemo Nov 06 '12 at 18:48

1 Answers1

3

Instead of using == use .equals() when comparing Strings.

if (input.equals( "3"))
        converter();
    if (input .eqauls( "4"))
        sfer();

(or)

Convert input to integer and do ==

    if( Integer.valueOf(input) == 3)
      {
     ......
      }

== looks for reference equality. equals() looks for object equality.

kosa
  • 65,990
  • 13
  • 130
  • 167
  • Aha! :) Didn't know == didn't work with strings! Thanks! :) The first suggestion was the best I think, the other looks like an unnecisary work-around. – Danny Persson Nov 06 '12 at 19:03