Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-02-2008, 05:55 AM
Member
 
Join Date: Nov 2008
Posts: 11
Rep Power: 0
jralexander is on a distinguished road
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:

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++;

                }

                
            }
            }
        }

    
}
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 12-02-2008, 06:25 AM
Senior Member
 
Join Date: Sep 2008
Posts: 607
Rep Power: 1
Darryl.Burke is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-02-2008, 06:33 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 6,522
Rep Power: 9
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
db is talking about this.

Code:
while (loop == false){
And also this is very bad coding style for me. Do you know about logical compliment operator?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Someone helped you?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.
Help:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Resources:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Web:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Tips:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-02-2008, 06:57 AM
Senior Member
 
Join Date: Sep 2008
Posts: 607
Rep Power: 1
Darryl.Burke is on a distinguished road
Default
The entire chain of if (iter == ...) is redundant, as all the conditionals will be entered the first time through.

db
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 12-02-2008, 07:08 AM
Member
 
Join Date: Nov 2008
Posts: 11
Rep Power: 0
jralexander is on a distinguished road
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!
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT +2. The time now is 06:02 AM.



VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org