3

I've been trying to achieve this for a week with no success. I would like the program to print command line arguments in brackets like shown below (in one line):

---------- +++++++++++ --------- ++++++++ 
- orange - + apricot + - grape - + kiwi + 
---------- +++++++++++ --------- ++++++++ 
  • Command line arguments: orange, apricot, grape, kiwi
  • the brackets change from - for arguments with even indexes and + for arguments with odd indexes

But my code prints:

----------
- orange -
----------
+++++++++++
+ apricot +
+++++++++++
---------
- grape -
---------
++++++++
+ kiwi +
++++++++

My code:

public class A2 {

    public static void main(String[] args) {
        for (int i = 0; i < args.length; i++) {
            //1.line
            if (i % 2 == 0) {
                System.out.println("-".repeat(args[i].length()+4));
            } else {
                System.out.println("+".repeat(args[i].length()+4));
            }
            //2.line
            if (i % 2 == 0) {
                System.out.printf("- %s -\n", args[i]);
            } else {
                System.out.printf("+ %s +\n", args[i]);
            }
            //3.line
            if (i % 2 == 0) {
                System.out.println("-".repeat(args[i].length()+4));
            } else {
                System.out.println("+".repeat(args[i].length()+4));
            }
        }
    } 
}

I tried different print methods, like System.out.print() and getting rid of \n, but it only messes up the brackets around words, so I believe I have to do something at the for loop level, but I am a complete java newbie and I am lost. I thought about maybe adding another variable to solve the problem, but I can't wrap my brain around how to do that in this case and whether it will even solve the problem.

Abra
  • 19,142
  • 7
  • 29
  • 41
Eva
  • 41
  • 4
  • 1
    If `System.out.print` isn't working, you could pretty easily use a StringBuilder. https://stackoverflow.com/a/5234160/8382028 – ViaTech Feb 26 '22 at 12:07
  • 1
    This is not a Java problem, but a problem of a sound algorithm. You obviously first need the number of arguments and the lengths of them. Wit that information you can first assemble the upper line (containing only `-` and `+`), then the middle line, and then the lower line. – Seelenvirtuose Feb 26 '22 at 12:14
  • 1
    You can only print line by line, so building your "blocks" first will not work. You need your algorithm to print `---------- +++++++++++ --------- ++++++++` first, then `- orange - + apricot + - grape - + kiwi +` and lastly `---------- +++++++++++ --------- ++++++++` again – QBrute Feb 26 '22 at 12:16
  • Thank you, I am looking up StringBuilder right now as we haven't learned about it yet, but it looks promising. – Eva Feb 26 '22 at 12:21
  • If I understand correctly (which I am not 100% sure of) I have already tried the other two solutions in a way, but the program has to print bordered arguments like this no matter how many or what the arguments are... If I set the number of arguments and their length in advance it turns out to be the wrong solution, because the program doesn't work if ther is one argument less per say. But again, maybe I didn't understand and I will look into it, Thank you very much :) – Eva Feb 26 '22 at 12:24

3 Answers3

2

You need to have a separate for loop for each line that you want to print (rather than a single for loop that tries to print all three lines).

Note that since the first and last lines are identical, you could replace the first and last for loops with a call to a method that prints the line.

public class A2 {

    private static void printLine(String[] args) {
        for (int i = 0; i < args.length; i++) {
            if (i % 2 == 0) {
                System.out.print("-".repeat(args[i].length() + 4));
                System.out.print(" ");
            }
            else {
                System.out.print("+".repeat(args[i].length() + 4));
                System.out.print(" ");
            }
        }
        System.out.println();
    }

    public static void main(String[] args) {

        // 1.line
        printLine(args);

        // 2.line
        for (int i = 0; i < args.length; i++) {
            if (i % 2 == 0) {
                System.out.printf("- %s -", args[i]);
                System.out.print(" ");
            }
            else {
                System.out.printf("+ %s +", args[i]);
                System.out.print(" ");
            }
        }
        System.out.println();

        // 3.line
        printLine(args);
    }
}

The output I get for your sample arguments is:

---------- +++++++++++ --------- ++++++++ 
- orange - + apricot + - grape - + kiwi + 
---------- +++++++++++ --------- ++++++++ 
Abra
  • 19,142
  • 7
  • 29
  • 41
1

Here's my solution. The approach you want to take with this is to go line by line as with Java you can't go back to the previous line without third party libraries.

public static void main(String[] args)
{
    if(args.length > 0)
    {
        System.out.println(makeBorderLine(args));
        System.out.println(makeFruitLine(args));
        System.out.println(makeBorderLine(args));
    }
}

public static String makeFruitLine(String[] args)
{
    String fruitLine = "";

    for(int i = 0; i < args.length; i++)
    {
        if(i % 2 == 0)
        {
            fruitLine += "- " + args[i] + " - ";
        }
        else
        {
            fruitLine += "+ " + args[i] + " + ";
        }
    }

    return fruitLine;
}

public static String makeBorderLine(String[] args)
{
    String line1  = "-";
    String line2 = "+";
    String line = "";
    for(int i = 0; i < args.length; i++)
    {
        if(i % 2 == 0)
        {
            line += line1.repeat(args[i].length() + 4) + " ";
        }
        else
        {
            line += line2.repeat(args[i].length() + 4) + " ";
        }
    }

    return line;
}
0

Thanks to everyone who gave some clues or solutions for me to study.

In the end I understood what the problem of my code was and opted for a solution that might not be the most optimal one, but is the one I, a complete beginner, understand most at this moment and also one that covers only the stuff we were learning so far.

This was my code:

public class A2 {
       public static void main(String[] args){
           //1.line
           for (int i = 0; i < args.length; i++){
               if (i % 2 == 0) {
                   System.out.print("-".repeat(args[i].length()+4) + " "); 
               } else {
                   System.out.print("+".repeat(args[i].length()+4) + " "); 
               }
           }    
           System.out.println();
           //2.line
           for  (int i = 0; i < args.length; i++){   
               if (i % 2 == 0) {
                   System.out.printf("- %s -", args[i]);
               } else {
                   System.out.printf("+ %s +", args[i]);
               }
           }
           System.out.println();
           //3.line
           for  (int i = 0; i < args.length; i++){   
               if (i % 2 == 0) {
                  System.out.print("-".repeat(args[i].length()+4) + " "); 
               } else {
                   System.out.print("+".repeat(args[i].length()+4) + " ");
               }
           }        
           System.out.println();
   } 
}
´´´
Eva
  • 41
  • 4