2 Replies - 811 Views - Last Post: 11 November 2012 - 11:42 AM Rate Topic: -----

#1 AlexaM160   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 20-September 12

Having trouble with nested for loop.

Posted 11 November 2012 - 11:33 AM

Hey, I'm having trouble with a nested for loop. My goal of the program is you enter a length and a width, and it displays a rectangle or square of asterisks based on those 2 parameters. I tried using a nested for loop, but I get a program that displays an infinite line of stars it looks like. Here is my code

	import java.util.Scanner;
	 
	 
	public class Game {
	    public static void main(String[] args) {
	     int length;
	     int width;       
	    Scanner input = new Scanner (System.in);
	
	
	    System.out.print("Enter the length: ");
	    length = input.nextInt();
	    
	    System.out.print("Enter the width: ");
	    width = input.nextInt();
	    input.close();
	 
	    for (int i = 0; i < width; i++)
	    {
	    for (int j = 0; j < length; i++)
	    {
	    System.out.print("*");
	    }
	    System.out.println();
	    }
	  } 
	     
	}


Is This A Good Question/Topic? 0
  • +

Replies To: Having trouble with nested for loop.

#2 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Having trouble with nested for loop.

Posted 11 November 2012 - 11:36 AM

Check the second for loop for 'j' rather than 'i'.
Was This Post Helpful? 1
  • +
  • -

#3 AlexaM160   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 20-September 12

Re: Having trouble with nested for loop.

Posted 11 November 2012 - 11:42 AM

Ah, wow thank you. Very simple solution it seems, didn't even realize I'd made the small typo, must have "fat fingered" it as they say. Thanks Greg!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1