Printing multiple array elements concatenated in Java

Viewed 2678

The code below returns a strange result. The problem is somehow in Line 46. Adding a String as argument to println solves the issue

System.out.println("result" + arr[i] + arr[j]+ arr[k]);
System.out.print("\n" + arr[i] + arr[j]+ arr[k]);

I don't understand why println wouldn't work. Is it not possible to concatenate arrays elements without inserting a string in java?

import java.util.Scanner;
public class Main 
{
    public static void main(String Args[])
    {
        System.out.print("How many digits: ");
        Scanner obj = new Scanner(System.in);
        int n = obj.nextInt();
        int[] arr = new int[n];
        for(int i=0; i<n; i++)
        {
            System.out.print("Enter number "+ (i+1) +": ");
            arr[i]=obj.nextInt();
        }
        combinations(arr);
    }

    public static void combinations(int[] arr) {
        int count=0;
        for(int i=0; i<arr.length; i++) {
            for(int j=0; j<arr.length; j++) {
                for(int k=0; k<arr.length; k++) {
                    System.out.println(arr[i] + arr[j]+ arr[k]);//Line 46 
                    count++;
                }
            }
        }
        System.out.print("\n" + "Total combinations: "+ count);
    }
}
2 Answers
Related