Harshini Harshini - 1 year ago 124
Java Question

How to print multiple variable lines in java

I'm trying to print the test data used in webdriver test inside a print line in Java

I need to print multiple variables used in a class inside a

function (printf/println/ whatever).

Can you guys help me?

public String firstname;
public String lastname;

firstname = "First " + genData.generateRandomAlphaNumeric(10);

lastname = "Last " + genData.generateRandomAlphaNumeric(10);

I need those print in a print statement as:

First name: (the variable value I used)

Last name: (the variable value I used)

Using something like below gives the exact result.

But I need to reduce the number of
lines and use a more efficient way.

System.out.printf("First Name: ", firstname);
System.out.printf("Last Name: ", lastname);


Answer Source

You can do it with 1 println:

System.out.printf("First Name: %s\nLast Name: %s",firstname, lastname);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download