I have this code here I want to use. I basically want to use string and variable together but save it in a variable
Int a = 27;
Int b = 22;
String question = System.out.println("what is " + a + "+" + b);
System.out.println(question);
I have this code here I want to use. I basically want to use string and variable together but save it in a variable
Int a = 27;
Int b = 22;
String question = System.out.println("what is " + a + "+" + b);
System.out.println(question);
You can easily concat strings for a variable like you do it in System.out.println.
So
String question ="what is " + a + "+" + b;
would do the job.
You are trying to assign the return value of System.out.println("what is " + a + "+" + b);
to your variable question
, however, System.out.println
does not return anything. You have to first creat your variable, and then print it, like so :
Int a = 27;
Int b = 22;
String question = "what is " + a + "+" + b;
System.out.println(question);
Remove the System.out.println() from the string variable question. So, String question = "what is " + a + "+" + b; When u want toask the question print the variable question with System.out.println()
Try this instead, Use String concatenation:
Int a = 27;
Int b = 22;
String questionStr = "what is " + a + " + " + b;
System.out.println(questionStr);