Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).

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!

like image 343
Harshini Avatar asked May 10 '14 18:05

Harshini


People also ask

Can you print multiple variables in Java?

You can print multiple types of data by appending them using + symbol in system. out. println method in one line.

How do I print multiple variables?

Python print multiple variables To print multiple variables in Python, use the print() function. The print(*objects) is a built-in Python function that takes the *objects as multiple arguments to print each argument separated by a space.


2 Answers

You can do it with 1 printf:

System.out.printf("First Name: %s\nLast Name: %s",firstname, lastname); 
like image 175
Totò Avatar answered Oct 13 '22 09:10

Totò


Or try this one:

System.out.println("First Name: " + firstname + " Last Name: "+ lastname +"."); 

Good luck!

like image 21
Malakai Avatar answered Oct 13 '22 07:10

Malakai