Results 1 to 12 of 12
Thread: Replace a cell with Spaces
- 11-07-2010, 12:41 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 56
- Rep Power
- 0
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
-
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.
- 11-07-2010, 12:48 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 56
- Rep Power
- 0
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 addedLast edited by Fubarable; 11-07-2010 at 12:51 PM. Reason: Moderator Edit: Code tags added
-
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!
- 11-07-2010, 01:02 PM #5
Member
- Join Date
- Oct 2010
- Posts
- 56
- Rep Power
- 0
I'm not sure , as i am a beginner, but i presume it is an If statement?
-
- 11-07-2010, 01:20 PM #7
Member
- Join Date
- Oct 2010
- Posts
- 56
- Rep Power
- 0
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
-
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?
- 11-07-2010, 01:34 PM #9
Member
- Join Date
- Oct 2010
- Posts
- 56
- Rep Power
- 0
I tried this;
for (int i=0;i<middleCell;i++)
{
if i==middleCell
System.out.println(" ");
}
But it didn't work :(
-
What if you leave the for loop part of this unchanged:
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.Java Code:for (int row = 1; row <= height; row++) { for (int column = 1; column <= width; column++) System.out.print("[_]"); System.out.println(); } // for
- 11-07-2010, 02:20 PM #11
Member
- Join Date
- Oct 2010
- Posts
- 56
- Rep Power
- 0
I tired that didn't work, i dont think im doing it right. I dont entirely know what you mean.
-
Show us.
in pseudo codei dont think im doing it right. I dont entirely know what you mean.
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
-
problem with spaces in
By olli_m in forum IntelliJ IDEAReplies: 0Last Post: 11-24-2009, 05:55 PM -
Delete Empty Spaces...
By ohytheng in forum New To JavaReplies: 1Last Post: 04-15-2009, 09:59 PM -
How do i add spaces ~(very simple)
By soc86 in forum New To JavaReplies: 3Last Post: 11-02-2008, 02:01 AM -
Load URL that contains spaces?
By barkster in forum Java AppletsReplies: 0Last Post: 01-30-2008, 09:40 PM -
Help with a word, if it is divided by spaces
By baltimore in forum New To JavaReplies: 1Last Post: 08-07-2007, 06:31 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks