Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print 2 int(s) but not add them?

I know this seems like a stupid question (so excuse me). Basically this is what I want to do:

int a = 5;
int b = 3;

System.out.print(a+b);

this will give me 8, but is there a way other than putting an empty string inbetween for it to print 5 and 3 (and not by converting the int to a string)?

Thank you so much.

like image 242
F.A Avatar asked Jan 12 '15 15:01

F.A


2 Answers

The print method will simply convert its argument to a string and write out the result. Therefore, if you want to display the two numbers concatenated, you will need to do this yourself by converting them to a string (either explicitly, or using "" as you've already mentioned).

If you want to avoid building the string yourself, you'd probably need to use the printf() method:

System.out.printf("%d%d", a, b);
like image 174
Alan Avatar answered Oct 28 '22 15:10

Alan


try

System.out.print(a+""+b)

or

System.out.print(a+" "+b) 

if you want a space between them

like image 39
Dr. John A Zoidberg Avatar answered Oct 28 '22 13:10

Dr. John A Zoidberg