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 8 of 8

Thread: JOptionPane using If and Else

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default JOptionPane using If and Else

    I am creating a java program that would do some conversion, unfortunately i can't make it work. Can anyone help me? Here's my code
    package If_then_Else;
        import javax.swing.*;
     
    public class If_then_Else {
        static final double km_per_miles = 1.6093;
        static final double meter_per_miles = 1.6093;
        static final double feet_per_miles = 5279.85565107;
        static final double inches_per_miles = 63358.26781284;
        static final double cm_per_miles = 160930.0002446136;
     
        static final double miles_per_km = 0.6213882;
        static final double meter_per_km = 1000;
        static final double feet_per_km = 3280.8399;
        static final double inches_per_km = 39370.07;
     
        static final double miles_per_meter = 621.3882;
        static final double km_per_meter = 1000;
        static final double feet_per_meter = 3.280839;
        static final double inches_per_meter = 39.3700788;
        static final double cm_per_meter = 100;
     
        static final double miles_per_feet = 0.0001893991176;
        static final double km_per_feet = 0.0003048;
        static final double meter_per_feet = 0.3048;
        static final double inches_per_feet = 12;
        static final double cm_per_feet = 30.48;
     
        static final double miles_per_inches = 0.000015783254;
        static final double km_per_inches = 0.000025349;
        static final double meter_per_inches = 0.02534;
        static final double feet_per_inches = 0.08333;
        static final double cm_per_inches = 2.51;
     
        static final double miles_per_cm = 0.000006218797;
        static final double km_per_cm = 0.000004;
        static final double meter_per_cm = 0.004;
        static final double feet_per_cm = 0.032808386877;
        static final double inches_per_cm = 0.3937008;
     
        public static void main(String[] args) {
     
            System.out.println("Choose a Conversion:");
            System.out.println("1. Miles");
            System.out.println("2. Kilometers");
            System.out.println("3. Meter");
            System.out.println("4. Feet");
            System.out.println("5. Inches");
            System.out.println("6. Centimeters");
            System.out.println("7. Exit");
     
            String opt = JOptionPane.showInputDialog(null, "Choice:");
     
            if (opt == 1) {
                System.out.println("Number of Kilometers:");
     
           String milesStr;
           double km;
           double miles;
     
           milesStr = JOptionPane.showInputDialog(null, "Number of Kilometers:");
           miles = Double.parseDouble(milesStr);
     
           km = miles * km_per_miles;
     
           JOptionPane.showMessageDialog(null, "km " + km + " miles.");
     
            }
            else if (opt == 2){
            System.out.println("Number of Miles:");
            }
            else if (opt == 3){
            System.out.println("Number of Miles:");       
            }
            else if (opt == 4){
            System.out.println("Number of Miles:");       
            }
            else if (opt == 5){
            System.out.println("Number of Miles:");       
            }
            else if (opt == 6){
            System.out.println("Number of Miles:");       
            }
        }
     
    }
    If the first conversion works then i can do the rest. Please help me! ASAP!
    Last edited by copeg; September 29th, 2010 at 11:31 AM. Reason: Please use the code tags


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: JOptionPane using If and Else

    Please define can't make it work. Info such as this helps narrow the problem and results in a quicker response.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: JOptionPane using If and Else

    String opt = JOptionPane.showInputDialog(null, "Choice:");

    if (opt == 1) {
    This won't work.
    opt is a String variable and 1 is an int literal.
    If you want to compare a String to another String, use the equals method.
    If the literal 1 is to be a String enclose it in "1"

  4. The Following User Says Thank You to Norm For This Useful Post:

    Liuric (September 29th, 2010)

  5. #4
    Junior Member
    Join Date
    Sep 2010
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JOptionPane using If and Else

    Here's the almost finished java program of mine, the only thing that I can't do to it is that I don't know how to make it loop. Can you once more help me please?

    package If_then_Else;
    import javax.swing.*;

    public class If_then_Else {
    static final double km_per_miles = 1.6093;
    static final double meter_per_miles = 1.6093;
    static final double feet_per_miles = 5279.85565107;
    static final double inches_per_miles = 63358.26781284;
    static final double cm_per_miles = 160930.0002446136;

    static final double miles_per_km = 0.6213882;
    static final double meter_per_km = 1000;
    static final double feet_per_km = 3280.8399;
    static final double inches_per_km = 39370.07;

    static final double miles_per_meter = 621.3882;
    static final double km_per_meter = 1000;
    static final double feet_per_meter = 3.280839;
    static final double inches_per_meter = 39.3700788;
    static final double cm_per_meter = 100;

    static final double miles_per_feet = 0.0001893991176;
    static final double km_per_feet = 0.0003048;
    static final double meter_per_feet = 0.3048;
    static final double inches_per_feet = 12;
    static final double cm_per_feet = 30.48;

    static final double miles_per_inches = 0.000015783254;
    static final double km_per_inches = 0.000025349;
    static final double meter_per_inches = 0.02534;
    static final double feet_per_inches = 0.08333;
    static final double cm_per_inches = 2.51;

    static final double miles_per_cm = 0.000006218797;
    static final double km_per_cm = 0.000004;
    static final double meter_per_cm = 0.004;
    static final double feet_per_cm = 0.032808386877;
    static final double inches_per_cm = 0.3937008;

    public static void main(String[] args) {

    System.out.println("Choose a Conversion:");
    System.out.println("1. Miles");
    System.out.println("2. Kilometers");
    System.out.println("3. Meter");
    System.out.println("4. Feet");
    System.out.println("5. Inches");
    System.out.println("6. Centimeters");
    System.out.println("7. Exit");

    String opt = JOptionPane.showInputDialog(null, "Choice:");

    if (opt == null ? "1" == null : opt.equals("1")) {

    String milesStr;
    double km;
    double meters;
    double feet;
    double inches;
    double cm;
    double miles;

    milesStr = JOptionPane.showInputDialog(null, "Number of Miles:");
    miles = Double.parseDouble(milesStr);

    km = miles * km_per_miles;
    meters = miles * meter_per_miles;
    feet = miles * feet_per_miles;
    inches = miles * inches_per_miles;
    cm = miles * cm_per_miles;

    JOptionPane.showMessageDialog(null, "km " + km + " miles.");
    JOptionPane.showMessageDialog(null, "meters " + meters + " miles.");
    JOptionPane.showMessageDialog(null, "feet " + feet + " miles.");
    JOptionPane.showMessageDialog(null, "inches " + inches + " miles.");
    JOptionPane.showMessageDialog(null, "cm " + cm + " miles.");

    }
    else if(opt == null ? "2" == null : opt.equals("2")) {

    String kmStr;
    double km;
    double miles;
    double meters;
    double feet;
    double inches;

    kmStr = JOptionPane.showInputDialog(null, "Number of Kilometers:");
    km = Double.parseDouble(kmStr);

    miles = km * miles_per_km;
    meters = km * meter_per_km;
    feet = km * feet_per_km;
    inches = km * inches_per_km;

    JOptionPane.showMessageDialog(null, "km " + miles + " miles.");
    JOptionPane.showMessageDialog(null, "meters " + meters + " miles.");
    JOptionPane.showMessageDialog(null, "feet " + feet + " miles.");
    JOptionPane.showMessageDialog(null, "inches " + inches + " miles.");
    }
    else if(opt == null ? "3" == null : opt.equals("3")) {

    String metersStr;
    double km;
    double feet;
    double inches;
    double cm;
    double miles;
    double meters;

    metersStr = JOptionPane.showInputDialog(null, "Number of Meters:");
    meters = Double.parseDouble(metersStr);

    miles = meters * miles_per_meter;
    km = meters * km_per_meter;
    feet = meters * feet_per_meter;
    inches = meters * inches_per_meter;
    cm = meters * cm_per_meter;

    JOptionPane.showMessageDialog(null, "miles " + miles + " meter.");
    JOptionPane.showMessageDialog(null, "km " + km + " meter.");
    JOptionPane.showMessageDialog(null, "feet " + feet + " meter.");
    JOptionPane.showMessageDialog(null, "inches " + inches + " meter");
    JOptionPane.showMessageDialog(null, "cm " + cm + " meter.");

    }
    else if(opt == null ? "4" == null : opt.equals("4")) {

    String feetStr;
    double km;
    double meters;
    double inches;
    double cm;
    double feet;
    double miles;

    feetStr = JOptionPane.showInputDialog(null, "Number of Feet:");
    feet = Double.parseDouble(feetStr);

    miles = feet * miles_per_feet;
    km = feet * km_per_feet;
    meters = feet * meter_per_feet;
    inches = feet * inches_per_feet;
    cm = feet * cm_per_feet;

    JOptionPane.showMessageDialog(null, "miles " + miles + " feet.");
    JOptionPane.showMessageDialog(null, "km " + km + " feet.");
    JOptionPane.showMessageDialog(null, "meters " + meters + " feet.");
    JOptionPane.showMessageDialog(null, "inches " + inches + " feet");
    JOptionPane.showMessageDialog(null, "cm " + cm + " feet.");

    }
    else if(opt == null ? "5" == null : opt.equals("5")) {

    String inchesStr;
    double km;
    double meters;
    double feet;
    double cm;
    double inches;
    double miles;

    inchesStr = JOptionPane.showInputDialog(null, "Number of Inches:");
    inches = Double.parseDouble(inchesStr);

    miles = inches * miles_per_inches;
    km = inches * km_per_inches;
    feet = inches * feet_per_inches;
    meters = inches * meter_per_inches;
    cm = inches * cm_per_inches;

    JOptionPane.showMessageDialog(null, "miles " + miles + " inches.");
    JOptionPane.showMessageDialog(null, "km " + km + " inches.");
    JOptionPane.showMessageDialog(null, "feet " + feet + " inches.");
    JOptionPane.showMessageDialog(null, "meter " + meters + " inches");
    JOptionPane.showMessageDialog(null, "cm " + cm + " inches.");

    }
    else if(opt == null ? "6" == null : opt.equals("6")) {

    String cmStr;
    double km;
    double meters;
    double feet;
    double inches;
    double cm;
    double miles;

    cmStr = JOptionPane.showInputDialog(null, "Number of Centimeters:");
    cm = Double.parseDouble(cmStr);

    miles = cm * miles_per_cm;
    km = cm * km_per_cm;
    feet = cm * feet_per_cm;
    inches = cm * inches_per_cm;
    meters = cm * meter_per_cm;

    JOptionPane.showMessageDialog(null, "miles " + miles + " cm.");
    JOptionPane.showMessageDialog(null, "km " + km + " cm.");
    JOptionPane.showMessageDialog(null, "feet " + feet + " cm.");
    JOptionPane.showMessageDialog(null, "inches " + inches + " cm");
    JOptionPane.showMessageDialog(null, "meter " + meters + " cm.");

    }
    }

    }

    How could i also make the number i input to appear on the joptionpane? Such as "1 km = 1.6093mile "
    Last edited by Liuric; September 29th, 2010 at 11:59 PM.

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: JOptionPane using If and Else

    how to make it loop.
    Describe what you want to use a loop for.
    Two popular loops:
    for() which can use a variable that is initialized to a value and incremented and tests an ending condition
    while(condition) which loops while the condition is true.

  7. #6
    Junior Member
    Join Date
    Sep 2010
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JOptionPane using If and Else

    Quote Originally Posted by Norm View Post
    Describe what you want to use a loop for.
    Two popular loops:
    for() which can use a variable that is initialized to a value and incremented and tests an ending condition
    while(condition) which loops while the condition is true.
    I'll choose the while condition, in that case, does it mean i have to replace everything if i will use the while loop?

  8. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: JOptionPane using If and Else

    does it mean i have to replace everything if i will use the while loop
    It shouldn't. What you'll need is a way for the user to tell the program that he wants to exit the program. Usually that is done by having the user enter a special value to signal he's done. When the program sees that value, it exits the loop either by using a break statement or by setting the condition controlling the loop to false.

  9. #8
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: JOptionPane using If and Else

    Continued here:
    http://www.javaprogrammingforums.com...ptionpane.html

    db

Similar Threads

  1. Using Icons in JOptionPane
    By Jchang504 in forum AWT / Java Swing
    Replies: 3
    Last Post: September 19th, 2014, 02:28 PM
  2. [SOLVED] Scroll down in JOptionPane and window problems.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 60
    Last Post: June 16th, 2010, 12:04 PM
  3. Return result from JOptionPane to JFrame
    By cselic in forum AWT / Java Swing
    Replies: 7
    Last Post: May 13th, 2010, 01:17 PM
  4. How do i show all the values in one window(JOptionPane)??
    By Antonioj1015 in forum AWT / Java Swing
    Replies: 1
    Last Post: November 25th, 2009, 09:24 PM
  5. JOptionPane Question/ Printing
    By 03EVOAWD in forum AWT / Java Swing
    Replies: 2
    Last Post: August 31st, 2009, 09:17 AM