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

    Default Replace a cell with Spaces

    Hi guys, i have created a program that outputs and prints a rectangle when given the width and height as command line arguments. I've got it to add 1 row and column if the number of rows or columns is even, so that the rectangle as a middle cell. I want to take this cell out (i.e. replace with spaces).

    How would i get it to do this? Would i need to put in a loop or as an if statement.

    Kind Regards

    Shyam

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Shyamz1 View Post
    Hi guys, i have created a program that outputs and prints a rectangle when given the width and height as command line arguments. I've got it to add 1 row and column if the number of rows or columns is even, so that the rectangle as a middle cell. I want to take this cell out (i.e. replace with spaces).

    How would i get it to do this? Would i need to put in a loop or as an if statement.
    The solution all depends on your current program, one we have no access to. I think that you're assuming that we know a lot more about your program than we actually do. There are many talented coders here but none can read minds. Please give us more details about your current program, enough so that this question can be answered.

    For instance, how is your rectangle created? Does each "cell" consist of a String of some chars? Do you use nested for loops to create this? What does the code for creating a cell look like? etc...
    Last edited by Fubarable; 11-07-2010 at 12:50 PM.

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

    Default

    Java Code:
    public class PrintHoledRectangle
    {
      public static void main(String [] args)
      {
        int width = Integer.parseInt(args[0]);
        int height = Integer.parseInt(args[1]);
        int count = width * height;
        int middleCell = ((width*height)/2)+ 1;
        
       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++)
            System.out.print("[_]");
    	
    	
          System.out.println();
        } // for
        
        for (int i = 1; i < middleCell; i++)
        {
          i = middleCell;
          System.out.print("   ");
        
        }
        
        
      } // main
    } // class PrintHoledRectangle

    Moderator Edit: Code tags added
    Last edited by Fubarable; 11-07-2010 at 12:51 PM. Reason: Moderator Edit: Code tags added

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I've added code tags so that your code retains its formatting. To see how to do this yourself, please read the link in my signature.

    I see that you're creating your rectangle with nested for loops, and that each "cell" is displayed at a "_" String.

    So what you need to do is in the loop decide if you are the middle cell or not, and if you are in the middle cell print a space. This means that in essence you want to control program flow if a certain condition is true or not, and how do we do this in Java? What's your first guess for a solution?

    Luck!

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

    Default

    I'm not sure , as i am a beginner, but i presume it is an If statement?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Shyamz1 View Post
    I'm not sure , as i am a beginner, but i presume it is an If statement?
    That's why I'm trying to stretch your brain cells. :)

    If statement sounds promising. Why not give it a try and see what happens.

    Again, much luck!

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

    Default

    Wouldn't i have to make it count the cells first, or something. Because i have nothing to compare the value of MiddleValue to.

    Im stuck lol

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You'll know the mid number for row and the mid number for column, and you'll be able to tell when in your for loop row reaches its mid number and column reaches its mid number right?

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

    Default

    I tried this;

    for (int i=0;i<middleCell;i++)
    {
    if i==middleCell
    System.out.println(" ");
    }

    But it didn't work :(

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    What if you leave the for loop part of this unchanged:
    Java Code:
        for (int row = 1; row <= height; row++)
        {
          for (int column = 1; column <= width; column++)
            System.out.print("[_]");
    	
    	
          System.out.println();
        } // for
    but before the nested loop begins, set count equal to 0 and then inside the loop increment count by 1, and inside the loop check if count == middleCell.

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

    Default

    I tired that didn't work, i dont think im doing it right. I dont entirely know what you mean.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Shyamz1 View Post
    I tired that didn't work,
    Show us.

    i dont think im doing it right. I dont entirely know what you mean.
    in pseudo code

    Java Code:
    count is set to 0
    for every row
      for every column
         increment count by 1
         if count doesn't equal middleCell, print "_"
         else print " "
       end for every column
       println
    end for every row

Similar Threads

  1. problem with spaces in
    By olli_m in forum IntelliJ IDEA
    Replies: 0
    Last Post: 11-24-2009, 05:55 PM
  2. Delete Empty Spaces...
    By ohytheng in forum New To Java
    Replies: 1
    Last Post: 04-15-2009, 09:59 PM
  3. How do i add spaces ~(very simple)
    By soc86 in forum New To Java
    Replies: 3
    Last Post: 11-02-2008, 02:01 AM
  4. Load URL that contains spaces?
    By barkster in forum Java Applets
    Replies: 0
    Last Post: 01-30-2008, 09:40 PM
  5. Help with a word, if it is divided by spaces
    By baltimore in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:31 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
  •