SplattColor
SplattColor

Reputation: 115

How to print separate loops on the same line?

I am trying to sort this array in Ascending and Descending order, please help me out. I have two separate loops and i want to print both on the same line, If i cannot do this, please inform me a another method to do so. This is an assignment, if anyone was wondering.

    import java.util.Arrays;
    public class Lab10c{

        public static void main(String[] args) {
            System.out.println("Ascend\t\tDescend\n");
            String ss[] = new String[]{"Hi", "Hello", "Greetings", "Howdy", "Welcome"};
            Arrays.sort(ss);
            for ( int i = 0; i < ss.length; i++){
                System.out.print(ss[i] + "\n");
            }
                for (int r = ss.length -1; r>=0; r--){
                    System.out.println(  "\t\t" + ss[r]);   

                }
            }
        }

And this is the current printout:

    Ascend        Descend

    Greetings
    Hello
    Hi
    Howdy
    Welcome
            Welcome
            Howdy
            Hi
            Hello
            Greetings

I need it to print as the two separate columns lining up. Thanks! p.s Sorry if I'm unclear, I'm new to the site :)

Upvotes: 1

Views: 2125

Answers (3)

Mengjun
Mengjun

Reputation: 3197

Why the elemenets is printed as you mentioned?

In your code , for the first for-loop,

 for ( int i = 0; i < ss.length; i++){
        System.out.print(ss[i] + "\n");
    }

System.out.print(ss[i] + "\n"); equals to System.out.println(ss[i]), when a element in array is printed, a newLine() will print as well.

Then the second for-loop executes.

        for (int r = ss.length -1; r>=0; r--){
            System.out.println(  "\t\t" + ss[r]);   
        }

The element printed in this loop does not print from the first line in sonsole. That'way you got the result:

Ascend        Descend

Greetings
Hello
Hi
Howdy
Welcome
        Welcome
        Howdy
        Hi
        Hello
        Greetings

How to fix it?

You need to combine the 2 for-loop together like,

for (int i = 0; i < ss.length; i++) {
        //Print element in Ascend order
        System.out.print(ss[i]); 

        //Print element in Descend order in the same line
        System.out.println("\t\t" + ss[ss.length - i - 1]);
    }

Upvotes: 0

Jack
Jack

Reputation: 133557

To do this you must, in any case, get two elements on each iteration.

This means that at iteration i you will need to fetch both the element at i positions from the start and at i positions from the end.

You already have a loop that starts from the end and goes backward so you can easily see that this will do the trick:

for (int i = 0; i < ss.length; ++i) {
  int fromBegin = i;
  int fromEnd = ss.length - 1 - i;

  System.out.println(ss[fromBegin]+"\t"+ss[fromEnd]);
}

Both fromBegin and fromEnd will move in a specular way along the array.

Upvotes: 2

Prabhakaran Ramaswamy
Prabhakaran Ramaswamy

Reputation: 26094

do like this

        for ( int i = 0,j=ss.length-1;; i < ss.length; i++,j--){
            System.out.println(ss[i] + "\t\t"+ss[j]);
        }

Upvotes: 1

Related Questions