?
Solved

printing more than one variable using System.out.print

Posted on 2012-09-19
7
Medium Priority
?
577 Views
Last Modified: 2012-09-21
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
0
Comment
Question by:JRawabi
7 Comments
 
LVL 20

Accepted Solution

by:
chaitu chaitu earned 200 total points
ID: 38416696
System.out.println(x+"x..y.."+y);
0
 
LVL 20

Expert Comment

by:chaitu chaitu
ID: 38416706
prinln method you have only one argument.
0
 
LVL 12

Assisted Solution

by:Sharon Seth
Sharon Seth earned 200 total points
ID: 38416712
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 26

Assisted Solution

by:ksivananth
ksivananth earned 200 total points
ID: 38416727
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
 

Author Comment

by:JRawabi
ID: 38416745
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
 
LVL 26

Expert Comment

by:ksivananth
ID: 38416753
to print simply the variables,

System.out.println( String.format( "%1$s, %2$s", x, y ) ) ;
0
 
LVL 10

Assisted Solution

by:Gregg
Gregg earned 200 total points
ID: 38418971
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you are like me and like multiple layers of protection, read on!
The main intent of this article is to make you aware of ‘Exchange fail to mount’ error, its effects, causes, and solution.
An overview on how to enroll an hourly employee into the employee database and how to give them access into the clock in terminal.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question