I would like to concatenate a few variables into a string variable but I am unable to get it to work. When I compile it says \"not a statement\" and \"; expected.\"
String resW = a + " + " + b;
try this..
Instead of using resW
, you could try this:
public class QuickTester {
public static void main(String[] args) {
float a = 1;
float b = 2;
System.out.println(String.format("%.0f + %.0f", a, b));
System.out.println(String.format("%.2f + %.2f", a, b));
System.out.println(String.format("%.5f + %.5f", a, b));
}
}
Output:
1 + 2
1.00 + 2.00
1.00000 + 2.00000
Note:
String resW = String.format(...);
resW = a + " + " + b;
Use a plus sign to concatenate Strings.
It should allow an autoconversion from float
to String
, but if it doesn't, you can change the float
s to Float
s, and do:
resW = a.toString() + " + " + b.toString();