How to print multiple variable lines in Java

Harshini picture Harshini · May 10, 2014 · Viewed 173.7k times · Source

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 system.out.print function (printf/println/whatever).

public String firstname;
public String lastname;

firstname = "First " + genData.generateRandomAlphaNumeric(10);
driver.findElement(By.id("firstname")).sendKeys(firstname);

lastname = "Last " + genData.generateRandomAlphaNumeric(10);
driver.findElement(By.id("lastname")).sendKeys(lastname);

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 printf lines and use a more efficient way.

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

Thanks!

Answer

Totò picture Totò · May 11, 2014

You can do it with 1 printf:

System.out.printf("First Name: %s\nLast Name: %s",firstname, lastname);