1

I am doing part of this code where I have to print a square outline. the user enters the length(rows) and width(col) and together with that two char values which should alternate when they print. now I did the swapping trick but it has not worked properly. how can I improve it

here is the code my method caller is

String l = TextBox.textBoxString(5,5,'x','o');

    System.out.print(l);

and my method is

   public static String textBoxString(int rows, int cols, char c1, char c2) {
    String result= "";
    char temp2 = 0;
    for (int i = 0; i <= rows -1; i++){
        for (int j = 0; j <= cols-1; j++){
           
            if(i == 0 || j == 0 || i == rows-1 || j == cols-1){
           temp2 = c2;
           c2 = c1;
           c1 = temp2;
               result += c2 +"";
            }
            else{
                result += " ";
            }
        }
        result += "\n";
    }
    return result;
}

my method is printing this

xoxox
o   x
o   x
o   x
oxoxo

but I don't want the o in the same line as we can see there if the first is o then the last should be x. like this

xoxox
o   x
x   o
o   x
oxoxo

how am I supposed to do that tried putting the temporary swap in each for loop but it still gives me the wrong answer. any suggestions please.

and also the rows and columns change according to the user input so it can be 5,5 and noon of the chars should be repeating. a fellow coder helped me improve the code

1 Answers1

1

Only swap when you are appending a non-whitespace. But note that in the 5x5 case, you don't switch characters when you are in the first column, between the second row and the last row.

if(i == 0 || j == 0 || i == rows-1 || j == cols-1){
    if (i >= rows - 1 || i < 2 || j != 0) {
        // move the swapping code from outside to here
        temp2 = c2;
        c2 = c1;
        c1 = temp2;
    }
    result += c2 +"";
}
else{
    result += " ";
}

I would also recommend using a StringBuilder rather than a appending to a String, to avoid creating a lot of strings:

public static String textBoxString(int rows, int cols, char c1, char c2) {
  StringBuilder result = new StringBuilder();
  char temp2 = 0;
  for (int i = 0; i <= rows -1; i++){
    for (int j = 0; j <= cols-1; j++){
      if(i == 0 || j == 0 || i == rows-1 || j == cols-1){
        if (i >= rows - 1 || i < 2 || j != 0) {
          temp2 = c2;
          c2 = c1;
          c1 = temp2;
        }
        result.append(c2);
      }
      else{
        result.append(' ');
      }
    }
    result.append('\n');
  }
  return result.toString();
}
Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • Thank you Sweeper for that. I am new to this and I am not familiar with StringBuilder that is why I had to do this. Thank you. I think I will have to learn about StringBuilder –  Feb 22 '21 at 03:07
  • @Sweeper Has not compiler started implementing string concatenation via StringBuilder after ver. 8? – PM 77-1 Feb 22 '21 at 03:11
  • ok now after doing that the code works only when the user enters 3,5 but then when you enter 5,5 its shows this xoxox o x o x o x oxoxo –  Feb 22 '21 at 03:13
  • @PM77-1 You mean [this](https://stackoverflow.com/questions/46512888/how-is-string-concatenation-implemented-in-java-9)? Doesn't change the fact that it still creates a lot of strings. It doesn't use a single string builder for the whole method, does it? – Sweeper Feb 22 '21 at 03:14
  • @vardhanPatel How would you want 5, 5 to look? – Sweeper Feb 22 '21 at 03:15
  • I just edited the post again to show after you helped me. like in short the horizontal line should not repeat like they are reaping in the post in the middle –  Feb 22 '21 at 03:16
  • Thank you but is it possible if you tell me what is going in the if statement. it works but I just want to understand the login behind that –  Feb 22 '21 at 04:06
  • @vardhanPatel It's basically the negation of "you don't switch characters when you are in the first column, between the second row and the last row." because in the if statement, we are saying when we _should_ switch characters. `j != 0` refers to _not_ the first column, and `i >= rows - 1 || i < 2` means _not_ between the second row and the last row. – Sweeper Feb 22 '21 at 04:31