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

Thread: Loops only on the first statement

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

    Default Loops only on the first statement

    A little help please..It does only loop on the first conversion which is in "miles".

    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) {
     
            String opt = JOptionPane.showInputDialog(null, "1. Miles\n2. Kilometers\n3. Meters\n4. Feet\n5. Inches\n6. Centimters\n\nDesired Choice:");
            double cm;
     
            while (opt != null) {
     
           String milesStr;
           double km;
           double meters;
           double feet;
           double inches;
           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 + " miles\n" + meters + " miles\n" + feet + " miles\n" + inches + " miles\n" + cm + " miles");
     
           String kmStr;
     
           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,miles + " km\n" + meters + " km\n" + feet + " km\n" + inches + " km");
     
           String metersStr;
     
           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 + " meters\n" + km + " meters\n" + feet + " meters\n" + inches + " meters\n" + cm + " meters");
     
           String feetStr;
     
           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 + " feet\n" + km + " feet\n" + meters + " feet\n" + inches + " feet\n" + cm + " feet");
     
           String inchesStr;
     
           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 + " inches\n" + meters + " inches\n" + feet + " inches\n" + km + " inches\n" + cm + " inches");
     
           String cmStr;
     
           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,km + " cm\n" + feet + " cm\n" + feet + " cm\n" + inches + " cm\n");
     
     
            }
        }
    }
    Last edited by JavaPF; October 1st, 2010 at 08:08 AM. Reason: Please use highliught tags. See my sig


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Loops only on the first statement

    Is the aim of this application to pick a conversion type, input a number and then it displays the result?

    Why are you using a while loop? Do you want it to do every single conversion in a row?

    Please give me a better idea of the functionality of the application.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

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

    Default Re: Loops only on the first statement

    Quote Originally Posted by JavaPF View Post
    Is the aim of this application to pick a conversion type, input a number and then it displays the result?

    Why are you using a while loop? Do you want it to do every single conversion in a row?

    Please give me a better idea of the functionality of the application.
    Yes that's what is supposed to do. If I pressed 1 ( for the computation of miles )then I will input the number of Miles then it'll show the results under miles. After it then it should go back to the main menu which is to choose again that is either option 1 to or 6, but if none is entered then the loop will exit.

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

    Default Re: Loops only on the first statement

    Quote Originally Posted by JavaPF View Post
    Is the aim of this application to pick a conversion type, input a number and then it displays the result?
    Yes that's what is supposed to do. If I pressed 1 ( for the computation of miles )then I will input the number of Miles then it'll show the results under miles. After I chose a single conversion then it should go back to the main menu which is to choose again that is either option 1 to or 6, but if none is entered then the loop will exit.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,613
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Loops only on the first statement

    Have you tried playing computer with your code by manually stepping thru it step by step and seeing why it does what it does?
    You must go slow and carefully check what each statement does.

    What needs to change for the code execution to exit the while loop?
    Where is that change made?
    Last edited by Norm; October 2nd, 2010 at 07:40 AM.

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

    Liuric (October 3rd, 2010)

Similar Threads

  1. Array and loops.
    By Melawe in forum Loops & Control Statements
    Replies: 46
    Last Post: August 15th, 2010, 03:49 PM
  2. Java Loops help please?
    By Sarah-Perkin in forum Loops & Control Statements
    Replies: 2
    Last Post: December 6th, 2009, 01:52 PM
  3. Program with for loops help
    By ixjaybeexi in forum Loops & Control Statements
    Replies: 23
    Last Post: October 8th, 2009, 10:05 AM
  4. need help with loops plz
    By Kilowog in forum Loops & Control Statements
    Replies: 4
    Last Post: September 28th, 2009, 08:11 AM
  5. Replies: 11
    Last Post: April 29th, 2009, 03:12 AM