Results 1 to 5 of 5
Thread: Loop Problem
- 12-02-2008, 05:55 AM #1
Member
- Join Date
- Nov 2008
- Posts
- 11
- Rep Power
- 0
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++; } } } } }
- 12-02-2008, 06:25 AM #2
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
- 12-02-2008, 06:33 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
db is talking about this.
And also this is very bad coding style for me. Do you know about logical compliment operator?Java Code:while (loop == false){
- 12-02-2008, 06:57 AM #4
The entire chain of if (iter == ...) is redundant, as all the conditionals will be entered the first time through.
db
- 12-02-2008, 07:08 AM #5
Member
- Join Date
- Nov 2008
- Posts
- 11
- Rep Power
- 0
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
-
Need help with While Loop
By mrdestroy in forum New To JavaReplies: 14Last Post: 10-20-2008, 02:29 PM -
Problem to use different for loop to add up
By matt_well in forum New To JavaReplies: 6Last Post: 08-03-2008, 10:24 PM -
Beginner's Problem on Loop/If statement
By obdi in forum New To JavaReplies: 2Last Post: 07-07-2008, 01:41 AM -
For loop problem
By mcal in forum New To JavaReplies: 32Last Post: 01-25-2008, 03:51 PM -
eternal loop problem
By sandor in forum New To JavaReplies: 3Last Post: 04-29-2007, 03:55 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks