Java: System. Out. Println Concatenating Something Within a String (Really Easy Question)

I want my output to be Candy[1]. counterInt = 1

But my compiler isn't taking this code:

   System.out.println("Candy["+counterInt"]");

What can I do to have this variable appear within the string Candy[]?

Candy[] is not an array, it's a string.

5 Answers

You must put a + after counterInt:

System.out.println("Candy["+counterInt+"]");
0
System.out.println("Candy["+counterInt+"].counterInt="+counterInt);

you should have System.out.println("Candy[" + counterInt + "].counterInt = " + counterInt); to get Candy[1]. counterInt = 1;

You are missing a + after CounterInt.

System.out.println("Candy["+counterInt+"]");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Maya Lin-Takahashi

Maya Lin-Takahashi

Consumer Tech & Gadget Reviewer

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.

Share this article
Twitter Facebook Pinterest