How to print multiple variable lines in Java
Asked Answered
S

5

33

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!

Sisterinlaw answered 10/5, 2014 at 18:4 Comment(1)
The question at hand has nothing to do with Selenium. Remove this tag (and the part of the code that uses Selenium), and you will get an answer instantly...Bobbybobbye
H
61

You can do it with 1 printf:

System.out.printf("First Name: %s\nLast Name: %s",firstname, lastname);
Hickox answered 11/5, 2014 at 1:27 Comment(0)
S
18

Or try this one:

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

Good luck!

Spidery answered 12/5, 2014 at 17:35 Comment(0)
D
4
System.out.println("First Name: " + firstname);
System.out.println("Last Name: " + lastname);

or

System.out.println(String.format("First Name: %s", firstname));
System.out.println(String.format("Last Name: %s", lastname));
Doubleness answered 10/5, 2014 at 18:35 Comment(1)
thanks.. can I integrate the lines together and print everything inside one system.out.println function? that's my concernSisterinlaw
C
1

You can create Class Person with fields firstName and lastName and define method toString(). Here I created a util method which returns String presentation of a Person object.

This is a sample

Main

public class Main {

    public static void main(String[] args) {
        Person person = generatePerson();
        String personStr = personToString(person);
        System.out.println(personStr);
    }

    private static Person generatePerson() {
        String firstName = "firstName";//generateFirstName();
        String lastName = "lastName";//generateLastName;
        return new Person(firstName, lastName);
    }

    /*
     You can even put this method into a separate util class.
    */
    private static String personToString(Person person) {
        return person.getFirstName() + "\n" + person.getLastName();
    }
}

Person

public class Person {

    private String firstName;
    private String lastName;

    //getters, setters, constructors.
}

I prefer a separate util method to toString(), because toString() is used for debug. https://mcmap.net/q/77031/-how-to-use-the-tostring-method-in-java

I had experience writing programs with many outputs: HTML UI, excel or txt file, console. They may need different object presentation, so I created a util class which builds a String depending on the output.

Colossae answered 21/6, 2017 at 9:35 Comment(0)
L
0

Suppose we have variable date , month and year then we can write it in the java like this.

int date=15,month=4,year=2016;
System.out.println(date+ "/"+month+"/"+year);

output of this will be like below:

15/4/2016

Londonderry answered 13/7, 2018 at 16:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.