Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: My program skipping a for loop

  1. #1
    Member
    Join Date
    Jun 2012
    Location
    Uppsala, Sweden
    Posts
    36
    Thanks
    10
    Thanked 1 Time in 1 Post

    Post My program skipping a for loop

    Hi, I'm making a game with random world generation. Everything is running smothly except when it comes down to drawing the blocks on screen. It has a for loop to loop through all the different blocks but it just skips the for loop!

    Here's the draw method that should draw all the blocks:

    /**
     * Draw all the block in the world on the screen.
    * 
    * @param Graphics2D g
    */
    public void draw(Graphics2D g) {
     
    	System.out.println("Drawing...");
    		for (int var0 = 0; var0 < world.length; var0++) {
    			for (int var1 = 0; var1 < world[var0].length; var1++) {
    				System.out.println("In nested for loop!");
    				world[var0][var1].draw(this, g, var0, (size.height - var1));
    			}
    		}
     
    	System.out.println("Finished drawing!");
    }

    I have seriously no idea what the problem is!

    Please help me!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: My program skipping a for loop

    it just skips the for loop!
    The reason a for loop would not execute the code inside of it is that the continue condition is not true.
    Print out the values of the variables used in the for loop to see what they are.
    Add a call to System.out.println() to show the values just before both of the for statements.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jun 2012
    Location
    Uppsala, Sweden
    Posts
    36
    Thanks
    10
    Thanked 1 Time in 1 Post

    Default Re: My program skipping a for loop

    You're right apparently the world.length variable = 0. But i have no idea why. world is a Block[][]
    defined in the constructor like this:

    public World(Dimension par0Dimension) {
     
    		size = par0Dimension;
    		world = new Block[par0Dimension.width][par0Dimension.height];
    		generators = new ArrayList<Generator>();
    		generators.add(new GeneratorWorld());
    		blocks = ImageHandler.getBlocks();
    	}

    and the constructor is called from a JPanel object like this:

    public void initAndStart() {
     
    		world = new World(new Dimension((getWidth() / 20), (getHeight() / 20)));
    		world.generate();
    		thread = new Thread(this);
    		thread.start();
    	}
    Last edited by Dr.Code; September 15th, 2012 at 11:41 AM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: My program skipping a for loop

    world.length variable = 0
    What are the values used to define the world array? Print those out to be sure they are not 0
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    Dr.Code (September 16th, 2012)

  6. #5
    Member
    Join Date
    Jun 2012
    Location
    Uppsala, Sweden
    Posts
    36
    Thanks
    10
    Thanked 1 Time in 1 Post

    Default Re: My program skipping a for loop

    You were right again apparently I had put the setSize() method after i called the initAndStart() method. Thank you very much!

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: My program skipping a for loop

    When the code is not doing what you expect, add some println statements to the code to see what the values are the program is working with.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Scanner skipping input
    By hblakek in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: February 21st, 2012, 01:44 PM
  2. Skipping Input Problem
    By Matty Alan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 15th, 2011, 07:45 AM
  3. [SOLVED] if else skipping
    By Scotty in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 25th, 2010, 08:04 PM
  4. need help prog skipping method
    By Pulse_Irl in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 11th, 2010, 08:57 AM
  5. Java error while using BufferedReader class to read a .txt document
    By Jchang504 in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: February 4th, 2009, 07:55 PM

Tags for this Thread