3

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);
 }
}
Rodrigue
3,6872 gold badges41 silver badges50 bronze badges
asked Aug 13, 2017 at 16:16
3
  • arr[i] is getting added in arr[j] ... You will need to add String object in between... arr[i] is int and java's automatic type casting converts the int+int into int not in String.. Commented Aug 13, 2017 at 16:17
  • 3
    Ask yourself the reverse question: how would you do if you wanted to get the sum of these three integers, if using the + operator concatenated them? Commented Aug 13, 2017 at 16:21
  • + can represent two operations in Java depending on operands. If both operands are numeric then it produces sum, if at least one of them is String, then it is concatenation. Commented Aug 13, 2017 at 16:23

2 Answers 2

5

This is because the + operator has different behaviours depending on its operands: either it adds numbers or it concatenates strings.

You have declared your array as [int]. This means that when you do:

arr[i] + arr[j]+ arr[k];

you are calculating the sum of three ints which returns an int. This is defined in the Java specification as:

The binary + operator performs addition when applied to two operands of numeric type, producing the sum of the operands.

However, when you write:

"result" + arr[i] + arr[j]+ arr[k];

because the first element is a String, Java will convert all the other elements into Strings and concatenate them all together.

This is described in the Java specification as:

If only one operand expression is of type String, then string conversion is performed on the other operand to produce a string at run time.

Finally, when you call System.out.println it will evaluate the expression given as parameter first, then check if its type is String and call toString on it if it is not.

answered Aug 13, 2017 at 16:23
2
  • 3
    "because one element is a String" is incomplete. It's because the first element is a String. If you had put the string last, the 3 numbers would still be added, and the sum concatenated with the string. Commented Aug 13, 2017 at 16:31
  • 1
    I have changed the wording. Note that, as described in the spec, if it is not the 1st element that is a String, conversion can still be forced by using parentheses, e.g. 1 + (2 + " convert") will print "12 convert". Commented Aug 13, 2017 at 16:46
1
System.out.println(arr[i] + arr[j]+ arr[k]);

When it runs, it finds ints next to each other so it sums them

So the rule is easy :

  • String + anythingElse : concatenate
  • Number + Number : sum (int, double, float, ...)

To avoid that you're mandatory to add empty String between or a separator :

System.out.println(arr[i] + ""+ arr[j] +""+ arr[k]);
System.out.println(arr[i] + "-"+ arr[j] +"-"+ arr[k]);
answered Aug 13, 2017 at 16:19
2
  • "When it runs, it finds ints next to each other so it sums them" No it doesn't. It concatenates them, because the first value is a string, the other values are converted to strings too, and all 4 values are concatenated. Commented Aug 13, 2017 at 16:37
  • @Andreas I just forgot to remove the result sure ^^ My explanation would have no sense with it, if you downvote I think you can remove so ^^ Commented Aug 13, 2017 at 16:43

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.