Home > Software engineering >  How to make these strings print beside each other
How to make these strings print beside each other

Time:06-10

    public static void main(String[] args) {

        String A = "           AAA               \n"
                  "              A:::A              \n"
                  "             A:::::A             \n"
                  "            A:::::::A            \n"
                  "           A:::::::::A           \n"
                  "          A:::::A:::::A          \n"
                  "         A:::::A A:::::A         \n"
                  "        A:::::A   A:::::A        \n"
                  "       A:::::A     A:::::A       \n"
                  "      A:::::AAAAAAAAA:::::A      \n"
                  "     A:::::::::::::::::::::A     \n"
                  "    A:::::AAAAAAAAAAAAA:::::A    \n"
                  "   A:::::A             A:::::A   \n"
                  "  A:::::A               A:::::A  \n"
                  " A:::::A                 A:::::A \n"
                  "AAAAAAA                   AAAAAAA";
        
        String B = "\nBBBBBBBBBBBBBBBBB  \n"
                  "B::::::::::::::::B  \n"
                  "B::::::BBBBBB:::::B \n"
                  "BB:::::B     B:::::B\n"
                  "  B::::B     B:::::B\n"
                  "  B::::B     B:::::B\n"
                  "  B::::BBBBBB:::::B \n"
                  "  B:::::::::::::BB  \n"
                  "  B::::BBBBBB:::::B \n"
                  "  B::::B     B:::::B\n"
                  "  B::::B     B:::::B\n"
                  "  B::::B     B:::::B\n"
                  "BB:::::BBBBBB::::::B\n"
                  "B:::::::::::::::::B \n"
                  "B::::::::::::::::B  \n"
                  "BBBBBBBBBBBBBBBBB ";
        
        
        System.out.print(A);
        System.out.print(B);
    
    }
}

here's a simplified version of what im trying to do, when I run it A appears on top of B, I want A and B to print out beside each other but I have no clue how. very new to this, probably a stupid question but i cant find anything on the web so I'm hoping someone here can help me out

CodePudding user response:

According to the Information that i have to this case, i could suggest you a Souloution like this.

This is maybe not the prettiest soloution, but it is simple to understand, and it works.

I would not choose printf because String.split does not remove the linebreak. So you have to remove the linebreak from A but not from B

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

        String A = "              AAA               \n"
                  "              A:::A              \n"
                  "             A:::::A             \n"
                  "            A:::::::A            \n"
                  "           A:::::::::A           \n"
                  "          A:::::A:::::A          \n"
                  "         A:::::A A:::::A         \n"
                  "        A:::::A   A:::::A        \n"
                  "       A:::::A     A:::::A       \n"
                  "      A:::::AAAAAAAAA:::::A      \n"
                  "     A:::::::::::::::::::::A     \n"
                  "    A:::::AAAAAAAAAAAAA:::::A    \n"
                  "   A:::::A             A:::::A   \n"
                  "  A:::::A               A:::::A  \n"
                  " A:::::A                 A:::::A \n"
                  "AAAAAAA                   AAAAAAA";

        String B = "\nBBBBBBBBBBBBBBBBB  \n"
                  "B::::::::::::::::B  \n"
                  "B::::::BBBBBB:::::B \n"
                  "BB:::::B     B:::::B\n"
                  "  B::::B     B:::::B\n"
                  "  B::::B     B:::::B\n"
                  "  B::::BBBBBB:::::B \n"
                  "  B:::::::::::::BB  \n"
                  "  B::::BBBBBB:::::B \n"
                  "  B::::B     B:::::B\n"
                  "  B::::B     B:::::B\n"
                  "  B::::B     B:::::B\n"
                  "BB:::::BBBBBB::::::B\n"
                  "B:::::::::::::::::B \n"
                  "B::::::::::::::::B  \n"
                  "BBBBBBBBBBBBBBBBB ";
        
        List<String> arrayA = List.of(A.split("\n"));
        List<String> arrayB = List.of(B.split("\n"));

        for(String line : arrayA){
           line = line.replaceAll("\n","");
        }
        for(int i = 0; i < arrayA.size(); i  ){
            System.out.println(arrayA.get(i) arrayB.get(i));
        }


    }

CodePudding user response:

If you split each character into lines, you can then iterate over the number of lines and print them "in parallel." A much simpler example follows.

class Test {
    public static void main(String[] args) {
        String box1 = " - \n"  
                      "| |\n"  
                      " - ";

        String box2 = " - \n"  
                      "|x|\n"  
                      " - ";

        String[] lines1 = box1.split("\n");
        String[] lines2 = box2.split("\n");

        for (int i = 0; i < lines1.length; i  ) {
            System.out.printf("%s %s\n", lines1[i], lines2[i]);
        }
    }
}

CodePudding user response:

you can use \n to determine each line and print it side by side:

final String[] aa = A.split("\n");
final String[] ba = B.split("\n");
IntStream.range(0, aa.length).forEach((int idx) -> System.out.printf("%s %s\n", aa[idx], ba[idx]));
  •  Tags:  
  • java
  • Related