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

2012/7/24

Execution just... skipping

darkmist255 darkmist255

2012/7/24

#
I've got some code written to take a string as input and separate it into multiple strings divided by commas. I've placed tons of outputs to the terminal to figure out where it's stopping and determined it only runs the For loop once, then nothing else happens. The game itself keeps running fine though, just without the tile being added. I can't for the life of me figure out what's going on.
public void addTile(String tileData)
    {   
        //tileData is "0,0,visible,potato,oranges,green,monadfg,manatee"
        String tileXst = tileData;
        tileXst = tileXst.substring(0, tileXst.indexOf(','));
        System.out.println("146: " + tileXst); //output "146: 0"
        String bufLineText = tileData.substring(tileXst.length() + 1);
        tileData = bufLineText;
        String tileYst = bufLineText;
        
        String working = new String();
        if(tileYst.contains(","))
        {
            tileYst = tileYst.substring(0, tileYst.indexOf(','));
            bufLineText = tileData.substring(tileYst.length() + 1);
            working = bufLineText;
            tileData = working;
            System.out.println("156: " + working); //output "156: visible,potato,oranges,green,monadfg,manatee"
        }
        System.out.println("158: " + tileYst); //output "158: 0"
        
        int tileX = Integer.parseInt(tileXst);
        int tileY = Integer.parseInt(tileYst);
        
        List<String> tileInfo = new ArrayList();
        
        System.out.println("For loop begins"); //This is always reached
        for(int i = 0; i < 50; i++) //50 times max
        {
            
            if(bufLineText.contains(","))
            {
                working = working.substring(0, working.indexOf(','));
                System.out.println("181: " + working); //output: "181: visible"
                tileInfo.add(working);
                bufLineText = bufLineText.substring(working.length() + 1);
            }
            else {
                System.out.println("for loop break"); //never reached
                tileInfo.add(bufLineText);
                break;
            }
            System.out.println("for loop: " + i); //output "for loop: 0" //there is no more output, this is where it just dies
        }
        System.out.println("for loop finished"); //never reached
    //Further code is irrelevant, it just adds the tile to the world
}
danpost danpost

2012/7/24

#
Line 33 should be:
working = bufLineText.substring(0, bufLineText.indexOf(','));
bourne bourne

2012/7/24

#
You can always step through it with the debugger.
danpost danpost

2012/7/24

#
Better than the 'for' loop would be this:
bufLineText = bufLineText + ",";
while (!"".equals(bufLineText)
{
    tileInfo.add(bufLineText.substring(0, bufLineText.indexOf(',')));
    bufLineText = bufLineText.substring(bufLineText.indexOf(',') + 1);
}
darkmist255 darkmist255

2012/7/24

#
@bourne how do you use the debugger? I've never used it, it would probably be great help in some cases. @danpost I was originally going to use a while statement, but my while statement was "risky" if you know what I mean. Yours looks like it will work, thanks!
bourne bourne

2012/7/24

#
In your class code, when compiled, you can click on the left side where the line numbers are - which creates a stop sign. When your program runs across the lines of code with stop signs, it will pause the execution and the debugger will pop up (displaying all statuses of variables and options to step through the execution)
darkmist255 darkmist255

2012/7/24

#
Wow, that will make my life much easier in the future, thanks :D! Also, it's up and running now, thanks guys!
bourne bourne

2012/7/24

#
The controls work like this, Step: executes line of code and goes to the next line. Step into: if a method is called in the line, it will take you to the beginning of the method to step through Continue: Resume execution (maybe running into another stop sign later on) Terminate: Ends the program and restarts your program
bourne bourne

2012/7/24

#
No problem. It's a programmer's best friend ;)
SPower SPower

2012/7/24

#
For some extra information, those stop signs are called breakpoints.
You need to login to post a reply.