0

New to Java.

Does anyone know how I can print the following output:

The sum of the digits is 3 + 0 + 4 + 5 + 8 = 20

This is my print line:

System.out.print("The sum of the digits is: " + num1 + num2 + num3 + num4 + num5 + sum);

I want to get the + sign to display, but for some reason I get errors.

Any assistance is appreciated.

maio290
  • 6,440
  • 1
  • 21
  • 38
garro12
  • 19
  • 1

1 Answers1

0

There is a difference between + and "+". The first is used to combine Strings in this case and the second is a String with the value of a plus sign. So a simple plus in a String would look like this:

num1 + " + " + num2 + " = " + solution
Japhei
  • 565
  • 3
  • 18
  • Thanks for the quick response. I changed it but I get a different error now - Syntax error on token. This is the change I made: System.out.println("The sum of the digits is: " + num1 + " + " num2 + " + " num3 + " + " num4 + " + " num5 + " = " sum); – garro12 Jul 03 '22 at 22:06
  • 1
    @garro12 you are missing a + before num2 , this is the correct ans : System.out.print( "The sum of the digits is: " + num1 + " + " + num2 + " + " + num3 + " + " + num4 + " + " + num5 + " = " + sum); – Gurkirat Singh Guliani Jul 03 '22 at 22:09
  • You missed some `+` between `"+"` and `numX`. Note that you have to put a `+` between every part, reguardless of written String or variable. – Japhei Jul 03 '22 at 22:10