This site requires JavaScript, please enable it in your browser!
Greenfoot back
Shane024
Shane024 wrote ...

2015/4/20

Can anyone help me trouble shoot a problem with an unintended infinite loop?

Shane024 Shane024

2015/4/20

#
Need Help urgently. We were required to create a game as an assignment given no pre knowledge of Looping structures and Arrays (These are project requirements) in greenfoot. These aspects are to be taught to us after our assignment is given in and as a result of this I find myself experiencing a few problems in creating the program. The error message appearing on the gui says the following: "The constructor for the world is taking a long time. You may have an infinite loop." My code is below. Could anyone point me towards the right direction or perhaps explain what i did incorrectly?
public Stage1()
    {    
    super(600, 400, 1);
    Scanner Kb = new Scanner(System.in);
    int[] arrayNums = {0,1,2,3,4,5,6,7,8,9,10,11,12};
    
    for (int i = 0 ; i <= 10 ; i++)
    {
           int num1 = arrayNums[Greenfoot.getRandomNumber(12)];//get random array num position
           int num2 = arrayNums[Greenfoot.getRandomNumber(12)];//get random array num position
           JOptionPane.showInputDialog("What is "+num1+" added to "+num2+"?");//prompt for input
           int answer = Kb.nextInt();//accepting keyboard input in a variable
           int result = num1 + num2;
           int points = 0;
           
           if(answer==result)
           {
            JOptionPane.showMessageDialog(null,"Good Job!","",JOptionPane.INFORMATION_MESSAGE);
            points += 1;
           }
           if(points >=7)
           {
           JOptionPane.showMessageDialog(null,"You completed this level!Want to go to the next one?","",JOptionPane.INFORMATION_MESSAGE);
           int response = +JOptionPane.showConfirmDialog(null, "Do you want to continue to level 2?", "Confirm",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
           if( response == JOptionPane.YES_OPTION)
           {
           
           }
           else
           {
           Greenfoot.stop(); 
           }
           }

    }
}

}
I'd appreciate your help very much!
lordhershey lordhershey

2015/4/20

#
Why would you prompt for information inside of the constructor? Looks like you have a driver loop inside of the constructor and this would lead to many unhappy things. Can you describe how you are hoping this game would run?
Shane024 Shane024

2015/4/20

#
well it's meant to go pretty much like this, two random numbers are selected from the array using the get random number method, the user needs to answer the question to the sum of these two numbers, the program then needs to calculate the correct answer and compare it to the answer typed in by the user. based on correct answers the points variable then increases by one. once 7 out of 10 questions are answered correctly the program should prompt to go to the next stage. if yes is selected, go to next stage, changing the background image. if no is selected, end the program.
Super_Hippo Super_Hippo

2015/4/20

#
What happens if you add these lines between lines 3 and 4?
}

public void act()
{
Oh and another thing. I think 'Greenfoot.stop()' stops the scenario after this act, so you could use 'break' at its position and put the 'Greenfoot.stop' at the end.
Shane024 Shane024

2015/4/20

#
Super_Hippo wrote...
What happens if you add these lines between lines 3 and 4?
}

public void act()
{
Oh and another thing. I think 'Greenfoot.stop()' stops the scenario after this act, so you could use 'break' at its position and put the 'Greenfoot.stop' at the end.
I added those lines but unfortunately it still gives me the same error. I also edited the code according to the break statement and Greenfoot.stop(); it all results in the same error message... the code now reads :
public Stage1()
    {    
    super(600, 400, 1);
    }
 
public void act()
{
    Scanner Kb = new Scanner(System.in);
    int[] arrayNums = {0,1,2,3,4,5,6,7,8,9,10,11,12};
    int x = 0;
    for( x =0; x<10; x++)
    {
           int num1 = arrayNums[Greenfoot.getRandomNumber(12)];//get random array num position
           int num2 = arrayNums[Greenfoot.getRandomNumber(12)];//get random array num position
           JOptionPane.showInputDialog("What is "+num1+" added to "+num2+"?");//prompt for input
           int answer = Kb.nextInt();//accepting keyboard input in a variable
           int result = num1 + num2;
           int points = 0;
           
           if(answer==result)
           {
            JOptionPane.showMessageDialog(null,"Good Job!","",JOptionPane.INFORMATION_MESSAGE);
            points += 1;
           }
           if(points >=7)
           {
           JOptionPane.showMessageDialog(null,"You completed this level!Want to go to the next one?","",JOptionPane.INFORMATION_MESSAGE);
           int response = +JOptionPane.showConfirmDialog(null, "Do you want to continue to level 2?", "Confirm",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
           if( response == JOptionPane.YES_OPTION)
           {
           
           }
           else
           {
           break;
           }
           }
        }

        Greenfoot.stop();
}
}
Shane024 Shane024

2015/4/20

#
I also have tried this code using the do while loop and it still gives me the same error/
Super_Hippo Super_Hippo

2015/4/20

#
What happens if you (1) comment out the whole act method or (2) comment out line 11? I didn't use the Scanner or JOptionPane class yet, so I am not really familiar with their methods. Oh, I found something... Move line 18 to line 10. (Line 10 isn't needed, just use 'int x=0' instead of 'x=0' in the next line like you did it in the beginning.) Right now, you set the points to 0 every time before checking for a correct result. Could you use Ctrl+Shift+I to have the auto layout in your code? This would make it easier to read.
danpost danpost

2015/4/20

#
The problem is the use of the Scanner class. Greenfoot intercepts all keystrokes, so creating a scanner for System.in does no good at all. Anyways, the JOptionPane input dialog returns the input string; so after removing all Scanner related code, change line 15 to:
String response = JOptionPane.showInputDialog("What is "+num1+" added to "+num2+"?");
Then, after making sure 'response' is not 'null', you can try parsing the string to an integer. Oh, and move line 18 up to before where the loop begins or it will be set to zero every time you iterate through the loop.
Shane024 Shane024

2015/4/21

#
Super_Hippo wrote...
What happens if you (1) comment out the whole act method or (2) comment out line 11? I didn't use the Scanner or JOptionPane class yet, so I am not really familiar with their methods. Oh, I found something... Move line 18 to line 10. (Line 10 isn't needed, just use 'int x=0' instead of 'x=0' in the next line like you did it in the beginning.) Right now, you set the points to 0 every time before checking for a correct result. Could you use Ctrl+Shift+I to have the auto layout in your code? This would make it easier to read.
i commented out the loop but even then, it gives me the error stating that there is an infinite loop.
danpost wrote...
The problem is the use of the Scanner class. Greenfoot intercepts all keystrokes, so creating a scanner for System.in does no good at all. Anyways, the JOptionPane input dialog returns the input string; so after removing all Scanner related code, change line 15 to:
String response = JOptionPane.showInputDialog("What is "+num1+" added to "+num2+"?");
Then, after making sure 'response' is not 'null', you can try parsing the string to an integer. Oh, and move line 18 up to before where the loop begins or it will be set to zero every time you iterate through the loop.
after making the changes you suggested, and also removing the scanner class from the program, it still gives me the same error. my code now reads:
public class Stage1 extends World
{

    /**
     *
     *
     * 
     */
    public Stage1()
    {    
        super(600, 400, 1);
    }

    public void ProgramStart()
    {
        int[] arrayNums = {0,1,2,3,4,5,6,7,8,9,10,11,12};
        int points = 0;
        for(int x =0; x<10; x++)
        {
            int num1 = arrayNums[Greenfoot.getRandomNumber(12)];//get random array num position
            int num2 = arrayNums[Greenfoot.getRandomNumber(12)];//get random array num position
            String userAnswer = JOptionPane.showInputDialog("What is "+num1+" added to "+num2+"?");//prompt for input
            int answer = 0;
            if(userAnswer != null)
            {
                answer = Integer.parseInt(userAnswer);
            }
            int result = num1 + num2;

           
            if(answer==result)
            {
                JOptionPane.showMessageDialog(null,"Good Job!","",JOptionPane.INFORMATION_MESSAGE);
                points += 1;
            }
            if(points >=7)
            {
                JOptionPane.showMessageDialog(null,"You completed this level!Want to go to the next one?","",JOptionPane.INFORMATION_MESSAGE);
                int response = +JOptionPane.showConfirmDialog(null, "Do you want to continue to level 2?", "Confirm",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
                if( response == JOptionPane.YES_OPTION)
                {

                    break;
                }
                else
                {
                    break;
                }
            }
        }  

        Greenfoot.stop();
    }
}
the imports i am currently running in the code are "import greenfoot.*;" and "import javax.swing.*;"
danpost danpost

2015/4/21

#
What exactly is the error message you are getting? is it still the one about your world constructor taking a long time?
Shane024 Shane024

2015/4/21

#
Shane024 wrote...
The error message appearing on the gui says the following: "The constructor for the world is taking a long time. You may have an infinite loop."
This one up here. its constantly giving me that error message.
Super_Hippo Super_Hippo

2015/4/21

#
Is 'Stage1' the only World subclass? If not, right-click on Stage1 and click 'new Stage1()' (or something similar). Is it still the same then? I mean, is ONLY this without anything else giving you the same error?
import greenfoot.*;

public class Stage1 extends World
{
    public Stage1()
    {    
        super(600, 400, 1);
    }
}
The other method isn't executed anyway, so it couldn't cause it.
You need to login to post a reply.