Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Java 2D Array Border Trouble

Good day/night. I'm having an issue with creating a border. Note: It's for a school project, and I'm almost done with it. In fact, the border is the ONLY thing I have left to complete. I say this because I don't want other users to think I'm trying to get people to do my work for me. I did everything, I just need help with this.

It's supposed to look like this:
+---+
| |
+---+

And in a scratch work file, I was able to break down the border using StringBuilder. Here's the code below:

[CODE lang="java" title="Border Making code"] public static char[][] createGrid(int size) {
char[][] grid = new char[size][size]; //refers to a variable that allows the user to choose the board's size

for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid.length; j++) {
grid[j] = ' ';
}
}
return grid;
}

public static String displayGrid(char[][] grid) {
StringBuilder builder = new StringBuilder(); //StringBuilder, strings that can be overriden
String section = "+-----";
//Puts everything together
//Prints diff sections of the grid
builder.append(printHeaderLine(grid));
builder.append(printDashedLine(grid, section));
builder.append(printGrid(grid, section));

return builder.toString();
}
//Prints the top with the spaced letters
public static StringBuilder printHeaderLine(char[][] grid) {
StringBuilder builder = new StringBuilder(); //creates a new Str builder

builder.append(" "); //Puts spaces into the char grid
for (int i = 0; i < grid.length; i++) {
builder.append((char) ((int) 'A' + i)); //Makes the Lettercolumns
builder.append(" ");
}
builder.append(System.lineSeparator());
return builder;
}
//Prints the grid
public static StringBuilder printGrid(char[][] grid, String section) {
StringBuilder builder = new StringBuilder();

for (int i = 0; i < grid.length; i++) {
builder.append(String.format("%3d", (i))); //Formats the row labels
builder.append(" ");
for (int j = 0; j < grid.length; j++) {
builder.append("+ ");
builder.append(grid[j]); //Puts in the empty cells
builder.append(" ");
}

builder.append("+");
builder.append(System.lineSeparator());
builder.append(printDashedLine(grid, section));
}

return builder;
}
//Prints the dashed lines.
public static StringBuilder printDashedLine(char[][] grid, String section) {
StringBuilder builder = new StringBuilder();

builder.append(" ");
for (int i = 0; i < grid.length; i++) {
builder.append(section);
}

builder.append("+");
builder.append(System.lineSeparator());

return builder;
}
[/CODE]

I did it in another file because I wanted to get the game done with (a running game is more important than a border). I finished the game and now I'm trying to integrate it with the Battleship file. I have a method that prints the board in the Battleship file:

[CODE lang="java" title="Prints game board"]public static void printBoard(char board[][]) {
System.out.print(" ");
for (int j = 0; j < size; j++) {
System.out.print((char)(j+65) + " "); //Displays letters for the columns
}

System.out.println();

for (int i = 0; i < size; i++) {
System.out.print(i); //Prints the row numbers
for (int j = 0; j < size; j++) {
System.out.print(board[j] + " ");
}
System.out.println();
}
System.out.println();
}[/CODE]

Any tips on how I can combine them? I tried for a few hours to no avail. I would either get errors or the game would run smoothly but the border would be gone. Also if you know a better way to tackle this problem, you can tell me since I'm up for suggestions. There's probably a more effective way to do this but I'm unable to figure it out! Thank you for your time, I appreciate it!
 
Good day/night. I'm having an issue with creating a border. Note: It's for a school project, and I'm almost done with it. In fact, the border is the ONLY thing I have left to complete. I say this because I don't want other users to think I'm trying to get people to do my work for me. I did everything, I just need help with this.

It's supposed to look like this:
+---+
| |
+---+

And in a scratch work file, I was able to break down the border using StringBuilder. Here's the code below:

[CODE lang="java" title="Border Making code"] public static char[][] createGrid(int size) {
char[][] grid = new char[size][size]; //refers to a variable that allows the user to choose the board's size

for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid.length; j++) {
grid[j] = ' ';
}
}
return grid;
}

public static String displayGrid(char[][] grid) {
StringBuilder builder = new StringBuilder(); //StringBuilder, strings that can be overriden
String section = "+-----";
//Puts everything together
//Prints diff sections of the grid
builder.append(printHeaderLine(grid));
builder.append(printDashedLine(grid, section));
builder.append(printGrid(grid, section));

return builder.toString();
}
//Prints the top with the spaced letters
public static StringBuilder printHeaderLine(char[][] grid) {
StringBuilder builder = new StringBuilder(); //creates a new Str builder

builder.append(" "); //Puts spaces into the char grid
for (int i = 0; i < grid.length; i++) {
builder.append((char) ((int) 'A' + i)); //Makes the Lettercolumns
builder.append(" ");
}
builder.append(System.lineSeparator());
return builder;
}
//Prints the grid
public static StringBuilder printGrid(char[][] grid, String section) {
StringBuilder builder = new StringBuilder();

for (int i = 0; i < grid.length; i++) {
builder.append(String.format("%3d", (i))); //Formats the row labels
builder.append(" ");
for (int j = 0; j < grid.length; j++) {
builder.append("+ ");
builder.append(grid[j]); //Puts in the empty cells
builder.append(" ");
}

builder.append("+");
builder.append(System.lineSeparator());
builder.append(printDashedLine(grid, section));
}

return builder;
}
//Prints the dashed lines.
public static StringBuilder printDashedLine(char[][] grid, String section) {
StringBuilder builder = new StringBuilder();

builder.append(" ");
for (int i = 0; i < grid.length; i++) {
builder.append(section);
}

builder.append("+");
builder.append(System.lineSeparator());

return builder;
}
[/CODE]

I did it in another file because I wanted to get the game done with (a running game is more important than a border). I finished the game and now I'm trying to integrate it with the Battleship file. I have a method that prints the board in the Battleship file:

[CODE lang="java" title="Prints game board"]public static void printBoard(char board[][]) {
System.out.print(" ");
for (int j = 0; j < size; j++) {
System.out.print((char)(j+65) + " "); //Displays letters for the columns
}

System.out.println();

for (int i = 0; i < size; i++) {
System.out.print(i); //Prints the row numbers
for (int j = 0; j < size; j++) {
System.out.print(board[j] + " ");
}
System.out.println();
}
System.out.println();
}[/CODE]

Any tips on how I can combine them? I tried for a few hours to no avail. I would either get errors or the game would run smoothly but the border would be gone. Also if you know a better way to tackle this problem, you can tell me since I'm up for suggestions. There's probably a more effective way to do this but I'm unable to figure it out! Thank you for your time, I appreciate it!

you're not too far off lol... I were you...HINT HINT.. I would put a conditional statement in there to check if you are currently iterating through the first column and the last, if so, you place a "|"... ;)
 
you're not too far off lol... I were you...HINT HINT.. I would put a conditional statement in there to check if you are currently iterating through the first column and the last, if so, you place a "|"... ;)


to give you a better picture ;)
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom