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

Thread: Can I have an if statement inside a while loop? or something similar?

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Can I have an if statement inside a while loop? or something similar?

    So i am writing a code to display the windchill temperature, the user enters a temp and windspeed and they are given the windchill temp. The thing is they have to enter a temp within -58F and 41F, I want it to make them keep entering a temp until it is valid, right now it just prompts them to re-enter once then if they enter an invlaid again it still works...

    heres the code

    import javax.swing.JOptionPane;
     
    public class Pg125 {
        public static void main(String[] args) {
     
            //Input and Output= Dialog
     
           String temp = JOptionPane.showInputDialog("Enter a temperature within -58F and 41F: ");
           Double temp1 = Double.parseDouble(temp); 
     
           //temp if
           if(temp1 < -58 || temp1 > 41){ 
              JOptionPane.showInputDialog("The temperature " + temp1 + " is invalid. Please enter a valid temperature"); 
            }//end of temp if
     
           String windspeed = JOptionPane.showInputDialog("Enter a wind speed of 2 or greater: ");
            Double windspeed1 = Double.parseDouble(windspeed);
     
            //windspeed if
             if(windspeed1 < 2) {
              JOptionPane.showMessageDialog(null, "The windspeed " + windspeed1 + " is invalid. Please click cancel and enter a valid windspeed"); 
            }//end of windspeed if    
     
            Double windchill = 35.74 + 0.6215*temp1 - 35.75*Math.pow(windspeed1, 0.16) + 0.4275*temp1*Math.pow(windspeed1, 0.16);
     
            //wind-chill if
           if(temp1 > -58 || temp1 < 41 && windspeed1 >= 2){
             JOptionPane.showMessageDialog(null, "The wind-chill temperature is " + windchill);
            }//end of wind-chill if
     
     
     
     
     
     
     
     
        }
     
    }

    I tried just changing the if into a while, but it just keeps asking them to enter a valid one even if it is valid... thanks.
    Last edited by ColeTrain; October 2nd, 2012 at 09:27 PM.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Can I have an if statement inside a while loop? or something similar?

    A basic do-while loop is one way to get something done to satisfy your conditions. The layout may resemble this:

    do {
    some thing correctly
    } while ( something was done wrong)

    If the something is done correctly the first time, the "loop" will not repeat.

    I see you have:
    (temp1 > -58 || temp1 < 41 && windspeed1 >= 2)
    before your output. I just want to mention that if you verify the input as you get it, you don't need to verify it again before you output. This is not always the case, but in this specific case the values are never modified.

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Can I have an if statement inside a while loop? or something similar?

    what do you mean "something correctly" and "something done wrong"? Could you provide an example please? Thanks for your response

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Can I have an if statement inside a while loop? or something similar?

    Quote Originally Posted by ColeTrain View Post
    what do you mean "something correctly" and "something done wrong"? Could you provide an example please? Thanks for your response
    I basically handed you an answer (of the many possible). Read over this link and utilize your favorite search engine on keywords "java loops".
    If you have questions after seeing the wealth of information, feel free to ask.
    Good luck with your project!

Similar Threads

  1. Timer inside loop
    By dove in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 26th, 2012, 09:57 AM
  2. How do I loop this if statement?
    By dunnage888 in forum Loops & Control Statements
    Replies: 5
    Last Post: February 10th, 2012, 11:16 AM
  3. Switch statement inside a for loop need help.
    By TheWhopper858 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 12th, 2011, 10:50 PM
  4. What can be legally inside FOR statement?
    By ice in forum Loops & Control Statements
    Replies: 7
    Last Post: January 13th, 2011, 11:31 AM
  5. Adding array elements inside a loop
    By Scotty in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 28th, 2010, 08:48 PM