Results 1 to 10 of 10
  1. #1
    Shyamz1 is offline Member
    Join Date
    Oct 2010
    Posts
    56
    Rep Power
    0

    Default Count each cell as it prints.

    Hi guys, I have a program that prints a rectangle when you type the width and height as command line arguments. I have got it to print out the rectangle but What i am trying to do is make it count each cell and see if its odd/even. If its even, i want it to add another row and column to make it odd So i can make a hole in the middle of the rectangle i.e. miss a cell out:

    int width = Integer.parseInt(args[0]);
    int height = Integer.parseInt(args[1]);
    int count;
    for (int row = 1; row <= height; row++)
    {
    for (int column = 1; column <= width; column++)
    System.out.print("[_]");
    // end the line.
    System.out.println();

    Any help?

    Kind regards

    Shyam

  2. #2
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    I'm coding this blind, so if it doesn't work I apologize. Maybe something like this would work?

    Java Code:
    int width = Integer.parseInt(args[0]);
    int height = Integer.parseInt(args[1]);
    int count = width * height;
    
    if((count % 2) == 0)
    {
         width += 1;  // choose either width
         height += 1; // or height to increment
    }
    
    for (int row = 1; row <= height; row++)
    {
         for (int column = 1; column <= width; column++)
              System.out.print("[_]");
    
         System.out.println();
    }
    Last edited by joshdgreen; 10-29-2010 at 12:41 AM.
    Sincerely, Joshua Green
    Please REP if I help :)

  3. #3
    Shyamz1 is offline Member
    Join Date
    Oct 2010
    Posts
    56
    Rep Power
    0

    Default

    Sounds about right, but i get an error:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at PrintHoledRectangle.main(Rectangle.java:6)

  4. #4
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    Could you post some of that code, otherwise we can't see the problem area.
    Sincerely, Joshua Green
    Please REP if I help :)

  5. #5
    Shyamz1 is offline Member
    Join Date
    Oct 2010
    Posts
    56
    Rep Power
    0

    Default

    public class Rectangle
    {
    public static void main(String [] args)
    {

    int width = Integer.parseInt(args[0]);
    int height = Integer.parseInt(args[1]);
    int count=width*height;

    if ((count % 2) != 0)
    width+=1;
    height+=1;

    for (int row = 1; row <= height; row++)
    {
    for (int column = 1; column <= width; column++)
    System.out.print("[_]");
    // end the line.
    System.out.println();
    }
    }

  6. #6
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    Where is printHoledRectangle?
    Sincerely, Joshua Green
    Please REP if I help :)

  7. #7
    Shyamz1 is offline Member
    Join Date
    Oct 2010
    Posts
    56
    Rep Power
    0

    Default

    sorry its Rectangle not printholedrectangle i sent you wrong program:


    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at Rectangle.main(Rectangle.java:6)

  8. #8
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    You aren't passing in any arguments from the command line. Try using a scanner to read in the width and height from the user: Scanner (Java 2 Platform SE 5.0)
    Sincerely, Joshua Green
    Please REP if I help :)

  9. #9
    Shyamz1 is offline Member
    Join Date
    Oct 2010
    Posts
    56
    Rep Power
    0

    Default

    Are you sure? Because i am giving command lijne arguments...

  10. #10
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    Do you have to do it using command line? Also, how are you passing them in?

    This is what I came up with, it's messy, but I did it fast:

    Java Code:
    import java.util.Scanner;
    
    public class JavaForumHelp
    {
    	public static void main(String [] args)
    	{
    		String output = "";
    		Scanner keyboard = new Scanner(System.in);
    		System.out.print("Width: ");
    		int width = keyboard.nextInt();
    		System.out.print("Height: ");
    		int height = keyboard.nextInt();
    		int count=width*height;
    
    		if ((count % 2) == 0 && height == width)
    		{
    			width+=1;
    			height+=1;
    		}
    		else if((count % 2) == 0 && (width % 2) == 0 && (height % 2) != 0)
    			width += 1;
    		else if((count % 2) == 0 && (height % 2) == 0 && (width % 2) != 0)
    			height += 1;
    		else if((count % 2) == 0 && (height % 2) == 0 && (width % 2) == 0)
    		{
    			height += 1;
    			width += 1;
    		}
    
    		for (int row = 1; row <= height; row++)
    		{
    			for (int column = 1; column <= width; column++)
    				output += "[_]";
    
    			output += "\n";
    		}
    		
    		System.out.print(output);
    	}	
    }
    Sincerely, Joshua Green
    Please REP if I help :)

Similar Threads

  1. Line Count
    By tim in forum NetBeans
    Replies: 1
    Last Post: 08-02-2009, 05:41 PM
  2. post count
    By codeflip in forum Forum Lobby
    Replies: 2
    Last Post: 03-12-2009, 03:20 AM
  3. Replies: 1
    Last Post: 10-20-2008, 08:35 AM
  4. Doclet that prints out all members of the class
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 12:06 AM
  5. Getting row count
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-11-2008, 09:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •