Thursday, August 18, 2022

[FIXED] how to show two variables output in one line in java?

Issue

Sytem.out.println("value of x , y : "+x +y);

Output will be "value of x,y : 1020" where x is 10 and y is 20

But I want to print this "value of x,y : 10 20"


Solution

Other than string concatenation , you can also use String.format to do this:

System.out.println(String.format("value of x & y : %d  %d" , x, y));

or System.out.printf:

System.out.printf("value of x & y : %d  %d\n" , x, y);


Answered By - Qiu Zhou
Answer Checked By - Cary Denson (PHPFixing Admin)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.