Results 1 to 5 of 5

Thread: Loop Problem

  1. #1
    jralexander is offline Member
    Join Date
    Nov 2008
    Posts
    11
    Rep Power
    0

    Default Loop Problem

    Hi,
    When I run this program I am working on I the program hangs When the dialog boxes reach the part where the user chooses what type of family member that member is. I think it is a loop problem but I am having a brain fart atm. Let me know if you need any more info. There is no error it just hangs as if I have entered an inifinite loop. Here the code:

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package genealogy;
    
    import javax.swing.JOptionPane;
    
    /**
     *
     * @author Jake
     */
    public class FamilyCreation {
        public FamilyCreation(){
            int iter = 0;
            boolean loop = false;
            int totalMembers = 0;
            int num = 0;
            String choiceOne, choiceTwo, choiceThree, choiceFour;
            while (loop == false){
                if (iter == 0){
                    String s = (String)JOptionPane.showInputDialog(null,"Enter Family Name: ",
                            "Family Creation",JOptionPane.PLAIN_MESSAGE);
                         String familyName = s;
                         iter++;
                         System.out.println(s);
                }
                if (iter == 1) {
                    String s = (String)JOptionPane.showInputDialog(null, "Enter Number of Family Members: (Maximum of Four) "
                            , "Family Creation", JOptionPane.PLAIN_MESSAGE);
                    totalMembers = Integer.parseInt(s);
                    System.out.println(s);
                    iter++;
                }
                if(iter == 2){
                    while(num != totalMembers){
                        if(num == 1){
                            Object[] possibilities =
                            {"Select a choice", "Mother", "Father", "Son", "Daughter"};
                            String s = (String)JOptionPane.showInputDialog(
                                null,
                                "Choose type of member: ",
                                "Family Creation",
                                JOptionPane.PLAIN_MESSAGE,
                                null,
                                possibilities,
                                "Select a choice");
                            System.out.println(s);
                            choiceOne = s;
                            num++;}
                        if(num == 2){
                            Object[] possibilities =
                            {"Select a choice", "Mother", "Father", "Son", "Daughter"};
                            String s = (String)JOptionPane.showInputDialog(
                                null,
                                "Choose type of member: ",
                                "Family Creation",
                                JOptionPane.PLAIN_MESSAGE,
                                null,
                                possibilities,
                                "Select a choice");
                            System.out.println(s);
                            choiceTwo = s;
                            num++;}
                        if(num == 3){
                            Object[] possibilities =
                            {"Select a choice", "Mother", "Father", "Son", "Daughter"};
                            String s = (String)JOptionPane.showInputDialog(
                                null,
                                "Choose type of member: ",
                                "Family Creation",
                                JOptionPane.PLAIN_MESSAGE,
                                null,
                                possibilities,
                                "Select a choice");
                            System.out.println(s);
                            choiceThree = s;
                            num++;}
                        if(num == 4){
                            Object[] possibilities =
                            {"Select a choice", "Mother", "Father", "Son", "Daughter"};
                            String s = (String)JOptionPane.showInputDialog(
                                null,
                                "Choose type of member: ",
                                "Family Creation",
                                JOptionPane.PLAIN_MESSAGE,
                                null,
                                possibilities,
                                "Select a choice");
                            System.out.println(s);
                            choiceFour = s;
                            num++;
                        }
                        iter++;
    
                    }
    
                    
                }
                }
            }
    
        
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    loop is initialized to false and never changed.

    sequential if blocks check for iter == 0, 1, 2

    iter is incremented and never reset

    What happens when iter == 3?

    db

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    db is talking about this.

    Java Code:
    while (loop == false){
    And also this is very bad coding style for me. Do you know about logical compliment operator?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    The entire chain of if (iter == ...) is redundant, as all the conditionals will be entered the first time through.

    db

  5. #5
    jralexander is offline Member
    Join Date
    Nov 2008
    Posts
    11
    Rep Power
    0

    Default

    I fixed it thanks though not exactly sure how I fixed it. Also I know the coding isn't very good this is my first run through but I always go back and make it more readable and run more efficiently. I just like to get it working first then go back and tweak. I rearranged my loops to get it to work but thanks for the help. As for the if(iter==) I was using that to keep from having all of the dialog boxes popping up at once although I'm not sure if they would or not. I'm guessing they wouldn't or you wouldn't have said that. I'll check that out later once I finish this part!

Similar Threads

  1. Need help with While Loop
    By mrdestroy in forum New To Java
    Replies: 14
    Last Post: 10-20-2008, 02:29 PM
  2. Problem to use different for loop to add up
    By matt_well in forum New To Java
    Replies: 6
    Last Post: 08-03-2008, 10:24 PM
  3. Beginner's Problem on Loop/If statement
    By obdi in forum New To Java
    Replies: 2
    Last Post: 07-07-2008, 01:41 AM
  4. For loop problem
    By mcal in forum New To Java
    Replies: 32
    Last Post: 01-25-2008, 03:51 PM
  5. eternal loop problem
    By sandor in forum New To Java
    Replies: 3
    Last Post: 04-29-2007, 03:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •