Cleiton Lima
Cleiton Lima

Reputation: 199

How to print the results of an array in a single line?

I would like to know if anybody could help me to understand why my array result does not come in one single line. The results of the code below is printed as:

[
1
2
3
4
5
6
7
8
9
10
]

Instead of [1 2 3 4 5 6 7 8 9 10].

Any thoughts on what I am doing wrong to the results not come in on line?

class RangeClass {

    int[] makeRange(int lower, int upper) {
      int arr[] = new int[ (upper - lower) + 1 ];

      for(int i = 0; i < arr.length; i++) {
        arr[i] = lower++;
      }
      return arr;
    }

    public static void main(String arguments[]) {
    int theArray[];
    RangeClass theRange = new RangeClass();

    theArray = theRange.makeRange(1, 10);
    System.out.println("The array: [ ");
    for(int i = 0; i< theArray.length; i++) {
      System.out.println(" " + theArray[i] + " ");
    }
    System.out.println("]");
    }
}   

Upvotes: 16

Views: 99234

Answers (9)

Pranav
Pranav

Reputation: 1646

Just make one small change to your code:

class ArrayDemo {

  int[] makeRange(int lower, int upper) {
    int arr[] = new int[ (upper - lower) + 1 ];

    for(int i = 0; i < arr.length; i++) {
      arr[i] = lower++;
    }
    return arr;
  }

  public static void main(String arguments[]) {
    int theArray[];
    ArrayDemo theRange = new ArrayDemo();

    theArray = theRange.makeRange(1, 10);
    System.out.print("The array: [ ");  //Remove println here
    for(int i = 0; i< theArray.length; i++) {
      System.out.print(" " + theArray[i] + " "); //Same here
    }
    System.out.println("]");
  }
}

Output array: [ 1 2 3 4 5 6 7 8 9 10 ]

Upvotes: 2

Jad Chahine
Jad Chahine

Reputation: 7149

You can do this in one line using Java 8

Assume you have this list of integers

List<Integer> lst = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8 ,9);

You can print all the items in one shot in this way

lst.forEach(nb -> System.out.print(nb + " "));

Or in this way

lst.forEach(System.out::print);

Result

1 2 3 4 5 6 7 8 9

Upvotes: 5

Kangkan Lahkar
Kangkan Lahkar

Reputation: 307

int []arr = {3, 4, 1, 7, 8, 5, 4, 11, 33, 21, 17, 15};

System.out.print(Arrays.toString(arr).replace("[","").replace("]", "").replace(",", ""));

Upvotes: 4

Adam Stelmaszczyk
Adam Stelmaszczyk

Reputation: 19837

You can use shorter version:

int theArray[] = {1, 2, 3};
System.out.println(java.util.Arrays.toString(theArray));

Upvotes: 52

Kunal Kishore
Kunal Kishore

Reputation: 235

Use System.out.print() at the place of System.out.println() in all the code because if you use System.out.println() a new line character is getting printed after our output on console each time it is called but if you use System.out.print() it will print what you are passing it as parameter. So Change your code to

class RangeClass {
    int[] makeRange(int lower, int upper) {
        int arr[] = new int[ (upper - lower) + 1 ];
        for(int i = 0; i < arr.length; i++) {
            arr[i] = lower++;
        }
        return arr;
    }

    public static void main(String arguments[]) {
        int theArray[];
        RangeClass theRange = new RangeClass();

        theArray = theRange.makeRange(1, 10);
        System.out.print("The array: [ ");
        for(int i = 0; i< theArray.length; i++) {
            System.out.print(" " + theArray[i] + " ");
        }
        System.out.print("]");
    }
}

Upvotes: 0

DeadlyJesus
DeadlyJesus

Reputation: 1533

Replace System.out.println by System.out.print like this:

System.out.print("The array: [ ");
for(int i = 0; i< theArray.length; i++) {
  System.out.print(" " + theArray[i] + " ");
}
System.out.println("]");

println add a line separator at the end of what you just printed.

Upvotes: 6

Azad
Azad

Reputation: 5055

for(int i = 0 ; i < theArray.length;i++){
 if(i==0)
   System.out.print("["+theArray[i]);
 else if(i==theArray.length-1)
   System.out.print(","+theArray[i]+"]");
 else
   System.out.print(","+theArray[i]);
}

Output :(for example)

[1,2,5,3,7]

Upvotes: 1

roger_that
roger_that

Reputation: 9791

System.out.println changes the line after printing the statement. That is why, your array elements are not coming in one line. Try using System.out.print instead and that would do the trick.

Upvotes: 0

Juned Ahsan
Juned Ahsan

Reputation: 68715

Use System.out.print instead of System.out.println

Upvotes: 4

Related Questions