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

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like