Harshini Harshini - 1 month ago 10
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

system.out.print
function (printf/println/ whatever).

Can you guys help me?

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

You can do it with 1 println:

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