printing more than one variable using System.out.print

Hi

This is really simple but I am very new to Java.
Can we print more than one variable using a single  System.out.print  like this

System.out.println(x,y);

It is giving an error.
Thanks
JRawabiAsked:
Who is Participating?
 
chaitu chaituCommented:
System.out.println(x+"x..y.."+y);
0
 
chaitu chaituCommented:
prinln method you have only one argument.
0
 
Sharon SethCommented:
System.out.println() accepts a single argument , either a primitive or an Object
 
So , we got to be giving a String by concatenating :
System.out.println("x is : " +x + " y is : "+y);
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
ksivananthCommented:
try String.format, it can take any number of parameters and helps you formatting output in desired way easily,

System.out.println( String.format( "X: %1$s, Y: %2$s", x, y ) ) ;
0
 
JRawabiAuthor Commented:
System.out.println(x+"x..y.."+y);

Why you have to concatenate to  "x..y" ?

Can't I just print more than 1 variable?
0
 
ksivananthCommented:
to print simply the variables,

System.out.println( String.format( "%1$s, %2$s", x, y ) ) ;
0
 
GreggCommented:
You can also use System.out.printf().

More examples and reference page found here.
public class Foo {
  public static void main(String[] args) {
    String a = "cow";
	 String b = "moon";
	 System.out.printf("The %s jumped over the %s.", a, b);
	 
	 a = "barn";
	 b = "chicken";
	 String c = "Tuesday";
	 
	 System.out.println();
	 System.out.printf("Last %s, the %s escaped from the %s.", c, b, a);
	 
  }
}

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.