-
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++;
}
}
}
}
}
-
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
-
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?
-
The entire chain of if (iter == ...) is redundant, as all the conditionals will be entered the first time through.
db
-
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!