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: while loop is not working right

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    8
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default while loop is not working right

    I am trying to run this code for class and I cant get the while loop to work properly.

    import javax.swing.JOptionPane;
    public class Modeling_001C_5WhileLoops {
    public static void main(String[] args) {

    // Define height and weight and assign values
    final int HEIGHT = 70;
    final double WEIGHT = 110.4999;
    String.format("%10.2f", 12.22);

    // Prompt user for height in inches
    String applicantsHeightString = JOptionPane.showInputDialog("Enter applicants height in inches" +
    " (must exceed 70 to move on)");
    double applicantsHeight = Double.parseDouble(applicantsHeightString);

    if (applicantsHeight >= HEIGHT) {
    JOptionPane.showMessageDialog(null, "Send applicant to get Weighed");
    }

    else if (applicantsHeight < HEIGHT) {
    JOptionPane.showMessageDialog(null, "Send her home...does not qualify");
    }
    // Prompt user of Weight in pounds
    if (applicantsHeight >= HEIGHT) {
    String applicantsWeightString = JOptionPane.showInputDialog("Enter Applicants Weight in pounds" +
    " (must not exceed 110.4 to qualify)");
    double applicantsWeight = Double.parseDouble(applicantsWeightString);

    if (applicantsWeight <= WEIGHT) {
    JOptionPane.showMessageDialog(null, "Congratulate her and Send to the first Photographer");
    }
    else if (applicantsWeight > WEIGHT) {
    JOptionPane.showMessageDialog(null, "Send her home! Does not meet weight requrements!");
    }

    // Define variables for Photographer 1
    if (applicantsHeight >= HEIGHT && applicantsWeight <= WEIGHT) {
    String photographer1String = JOptionPane.showInputDialog("Enter 1st Photographers Score");
    double photographer1 = Double.parseDouble(photographer1String);

    while (photographer1 >= 10.01) {
    JOptionPane.showInputDialog("Invalid score, please enter a score between 1 and 10.00");

    }

    // here is where I am having the problem. When the user enters a number greater than 10 it should as them to reenter the correct value. However when a new number is entered, it tells them it is invalid and reprompts them. it is an endless loop and will not go down to the if statement.//


    if (photographer1 >= 1 && photographer1 <= 10)
    JOptionPane.showMessageDialog(null, "Send to 2nd Photographer!");

    else if (photographer1 == 0) {
    JOptionPane.showMessageDialog(null, "No Pictures! IDon't want to break the camera" +
    "...it's my favorite");
    }

    // Define variables for Photographer 2
    if (photographer1 >= 1 && photographer1 <= 10) {
    String photographer2String = JOptionPane.showInputDialog("Enter 2nd Photographers Score");
    double photographer2 = Double.parseDouble(photographer2String);

    if (photographer2 >= 1 && photographer2 <= 10)
    JOptionPane.showMessageDialog(null, "Send to 3rd Photographer!");

    else if (photographer2 == 0) {
    JOptionPane.showMessageDialog(null, "What was the last guy thinking?" +
    " Send her home! NOW!!");
    }

    // Define variables for Photographer 3
    if (photographer2 >= 1 && photographer2 <= 10) {
    String photographer3String = JOptionPane.showInputDialog("Enter 3rd Photographers Score");
    double photographer3 = Double.parseDouble(photographer3String);

    if (photographer3 >= 1 && photographer3 <= 10)
    JOptionPane.showMessageDialog(null, "Calculate Average Score");

    else if (photographer3 == 0) {
    JOptionPane.showMessageDialog(null, "Waste of my time. How did she make it this far?");
    }

    // Define average and assign a value
    double averageScore = (photographer1 + photographer2 + photographer3) / 3;
    String.format("%10.2f", 12.22);

    if (averageScore <= 7.9 && photographer3 != 0) {
    JOptionPane.showMessageDialog(null, "Send Her Home!");
    }
    else if (averageScore >= 8 && averageScore <=8.7) {

    JOptionPane.showMessageDialog(null, "Average " + averageScore + "!" +
    "Too high risk, not worth spending your money!!");
    }
    else if (averageScore >= 8.8 && averageScore <= 9.3) {
    JOptionPane.showMessageDialog(null, " Average " + averageScore + "!" +
    " Low to Medium risk....has a pretty good chance to make you money!");
    }
    else if (averageScore >= 9.4 && averageScore <= 9.9) {
    JOptionPane.showMessageDialog(null, " Average " + averageScore + "!" +
    " Low risk to good investment....Excelant chance you will make money " +
    "bring her on board!");
    }
    else if (averageScore == 10) {
    JOptionPane.showMessageDialog(null, " Average " + averageScore + "!" +
    " Get her to sign a contract before she realizes how much she could take you for!!!");
    }
    }
    }
    }
    }
    }
    }



    Any help is greatly appreciated. I am only in my third week of my first class, so I don't know much.

    Kat


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,195
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: while loop is not working right

    cant get the while loop to work properly.
    Please explain what the loop does and what you want it to do that is different.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: while loop is not working right

    Please get rid of the color formatting and instead wrap your posted code above in [code=java] [/code] tags. Click on the "Edit Post" link at the bottom of your post above to edit it, and check the forum FAQ's for the gory details.

  4. #4
    Junior Member
    Join Date
    Feb 2013
    Posts
    8
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: while loop is not working right

    if (applicantsHeight >= HEIGHT && applicantsWeight <= WEIGHT) {				 
    			         String photographer1String = JOptionPane.showInputDialog("Enter 1st Photographers Score");
    			         double photographer1 = Double.parseDouble(photographer1String);  
     
    while (photographer1 >= 10.01) {
    JOptionPane.showInputDialog("Invalid score, please enter a score between 1 and 10.00");
    }
     
     
    if (photographer1 >= 1 && photographer1 <= 10)
    JOptionPane.showMessageDialog(null, "Send to 2nd Photographer!");
     
    else if (photographer1 == 0) {
    JOptionPane.showMessageDialog(null, "No Pictures! IDon't want to break the camera" +
    "...it's my favorite");

    When the user enters a number 0-10, it skips the while statement (which is good) and continues with the various if/else statements. The problem comes up when the user inters a number greater than 10, which prompts the while statement asking the user to input the correct value. What I want to happen is that when the correct value is entered, the loop is broken and the program moves down to the appropriate if/else statement. What is happening is the user will put in the new value and the while statement prompts them again asking for a correct value, and refuses to move on regardless of the value entered.

    I hope I posted the format correctly..
    Thanks

  5. #5
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: while loop is not working right

    Next time, please post only well formatted code. Your code is difficult to read even with code tags because it is all left justified (which should be obvious to you, no?). We don't ask for much, but we do ask that you try to put in some effort to help us help you, since we're all volunteers.

    As for your problem, in order for the while condition to change, it must change somewhere inside of the loop. Do you evern do this? Do you ever change the photographer1 variable inside of the loop? If not, think of how the loop will ever exit.

  6. #6
    Junior Member
    Join Date
    Feb 2013
    Posts
    8
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: while loop is not working right

    okay, I think that I understand what your saying, and I apologize for the formatting. It looks a whole lot different in eclipse then when it is posted here. I will try to get it better the next time I post. Is there a way to preview it prior to actually posting to check the format?

  7. #7
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: while loop is not working right

    Yes there's a preview. Click on Go Advanced and you'll see it as well as a button to update the preview.

    Getting back to your problem -- do you understand what I meant by updating the control variable inside of the loop? If not we'll need to discuss it further, but basically it's like the difference between the two loops below:

    int i = 0;
    while (i < 10) {
       System.out println("i is: " + i);
       i++;
    }

    The loop above will end when i reaches 10.

    int i = 0;
    int j = 0;
    while (i < 10) {
       System.out println("j is: " + j);
       j++;
       // i is never changed inside of the loop
    }

    The above loop will never end.

  8. #8
    Member
    Join Date
    Feb 2013
    Posts
    45
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: while loop is not working right

    why you use while loop?

    while (photographer1 >= 10.01) {
    JOptionPane.showInputDialog("Invalid score, please enter a score between 1 and 10.00");
    }
    In this place I think you use if condition that will work properly because you only check the variable is greater than the value or not.So you this code ,

    if(photographer1 >= 10.01) {
    JOptionPane.showInputDialog("Invalid score, please enter a score between 1 and 10.00");
    }
    I hope this will help to you
    Regards
    Android developer
    Trinay Technology Solutions
    http://www.trinaytech.com
    5705750475

Similar Threads

  1. Just can't see why this loop is not working.
    By Umbrafalx in forum Loops & Control Statements
    Replies: 6
    Last Post: September 12th, 2012, 09:29 AM
  2. Loop not working
    By Lurie1 in forum Loops & Control Statements
    Replies: 12
    Last Post: March 7th, 2012, 05:27 AM
  3. Can someone please tell me why my do while loop isn't working?
    By JavaAsh in forum What's Wrong With My Code?
    Replies: 14
    Last Post: October 20th, 2011, 03:52 PM
  4. Loop conditions not working
    By bmxershane in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 1st, 2011, 02:25 AM
  5. [SOLVED] Loop Breaking Not Working
    By aussiemcgr in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 25th, 2010, 09:06 PM