Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

222
Views
How to execute a block of code for every command line argument given, but print it in one line

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.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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 + 
---------- +++++++++++ --------- ++++++++ 
over 4 years ago · Santiago Trujillo Report

0

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;
}
over 4 years ago · Santiago Trujillo Report

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();
   } 
}
´´´
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!