Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How to add to a string format from another class

  1. #1
    Junior Member
    Join Date
    Apr 2021
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default How to add to a string format from another class

    I have 2 classes, the first class (person) has a string format and in the second one (Staff) i want to add some more variables to that string, how do i add to the string format?

    Class Person, this is the first class with the most information
     
     
     
    public String toString()
    {
        String str = String.format("\nName:         " + firstName + " " + lastName +
                                   "\nAddress:      " + address1 + 
                                   "\n              " + address2 +
                                   "\n              " + city + ", " + state + "  " + zipCode +
                                   "\nPhone:        " + phoneNumber +
                                   "\nEmail         " + email);
     
        return str;
    }


    Class Staff had more to it
    public String String()
    {
        String str = String.format("\nID            " + idNumber +
                                   "\nSalary        " + salary +
                                   "\nDepartment    " + department);
        return str;
    }

    Heres the class that gives information and runs it
    Person dave = new Person("Dave", "Hill", "Buttercup Ln", "", "Little Rock", "Arkansas", 67575, 555555, "dave@yahoo.com");
     
    Staff hank = new Staff("Hank", "Green", "57 Baker St", "", "Denver", "Colorado", 72120, 634839288, "hank@email.com", 2727, 10000.0, "work");
     
    System.out.println(john);
     
    System.out.println(hank);


    When it runs it prints out:

    Name: John Fox
    Address: Lane
    null
    Little Rock, Arkansas 63638
    Phone: 667733883
    Email john@email.com

    Name: Hank Green
    Address: 57 Baker St
    null
    Denver, Colorado 72120
    Phone: 634839288
    Email hank@email.com

    It prints exactly how i want but I want the second part to include the id number, salary, and department.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to add to a string format from another class

    I do not think the code is using the format method properly.
    The syntax for the method call is: format(String format, Object... args)
    It shows two parts, the format control String and the objects whose values are used by the format method.
    For example:
      System.out.println( String.format("%2.2f", 1.2345) );  // 1.23
    The format method takes the double value: 1.2345 and uses the format String %2.2f to build the String 1.23

    The posted code in post#1 is using String concatenation to build a String by using the concatenation operator: +

    how do i add to the string format?
    Use the String concatenation operator: +
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Extracting Numbers in Word Format From A String of Text
    By 4heatherlia in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 7th, 2014, 10:05 AM
  2. [SOLVED] Enforcing a format on a string?
    By Inked. in forum Java Theory & Questions
    Replies: 15
    Last Post: May 9th, 2014, 08:14 PM
  3. NumberFormat to Java String Format
    By zng690 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 6th, 2014, 04:57 PM
  4. Percentage with String.format
    By aussiemcgr in forum Java Theory & Questions
    Replies: 2
    Last Post: October 24th, 2013, 10:11 AM
  5. Replies: 6
    Last Post: June 3rd, 2013, 04:57 AM

Tags for this Thread