Home > Software design >  How to execute a block of code for every command line argument given, but print it in one line
How to execute a block of code for every command line argument given, but print it in one line

Time:02-26

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.

CodePudding user response:

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   
----------             ---------          

CodePudding user response:

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;
}
  • Related