I have a doubt which follows.
public static void main(String[] args) throws IOException{
int number=1;
System.out.println("M"+number+1);
}
Output: M11
But I want to get it printed M2 instead of M11. I couldn't number++ as the variable is involved with a for loop, which gives me different result if I do so and couldn't print it using another print statement, as the output format changes.
Requesting you to help me how to print it properly.
Add a bracket around your sum, to enforce the sum
to happen first. That way, your bracket
having the highest precedence will be evaluated first, and then the concatenation
will take place.
System.out.println("M"+(number+1));
Try this:
System.out.printf("M%d%n", number+1);
Where %n
is a newline
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With