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 3 of 3

Thread: toString method()

  1. #1
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default toString method()

    Well,I was hoping I could avoid this but here I am

    My problem is the following. In this Class person

     
    class Person {
      final String name;
      final String address;
      final String phoneNr;
     
      Person(String name, String address, String phoneNr) {
        this.name = name;
        this.address = address;
        this.phoneNr = phoneNr;
      }
     
      @Override
      public String toString() {
        return String.format("%-10s, %-20s, %-10s", name, address, phoneNr);
      }
     
    }

    There is this toString method.Now the instructions in the assignment say to rewrite it in the PhoneBookList class.


     
    class PhoneBookList implements PhoneBook {
        private int size = 0;
        Node head = null;
     
        private static final class Node {
            final Person person;
            Node next;
     
            Node(Person person) {
                this.person = person;
            }
        }
     
       public boolean insert(Person person) {
     
            Node n = new Node(person);
     
            if (head == null) {
                head = n;
                size++;
                return true;
            }
     
            Node current = head;
            Node prev = null;
            int comparison;
     
            while (current != null) {
                comparison = person.name.compareTo(current.person.name);
                if (comparison == 0) {
                    return false;
                } else if (comparison > 0) { 
     
                    if (current.next == null) { 
                        current.next = n;
                        break;
                    }
                    current = current.next;
                } else { 
                    if (prev == null) { 
                        Node oldHead = head;
                        head = n;
                        head.next = oldHead;
                        break;
                    }
                    prev.next = n;
                    n.next = current;
                    break;
                }
                prev = current;
                current = current.next;
            }
            size++;
            return true;
        }
     
    	public int size() {
    		Out.formatln("The phone book has: %d entries",size );
    		return size;
    	}
     
    	public boolean delete(String name) {
            if (head == null) {
                return false;
    		}
            if (head.person.name.compareTo(name) == 0) {
                head = head.next;
                size--;
                return true;
            }
     
            Node current = head;
            Node prev = head;
            while (current != null) {
                if (current.person.name.compareTo(name) == 0) {
                    prev.next = current.next;
                    size--;
                    return true;
                }
                prev = current;
                current = current.next;
            }
    		return false;
        }
     
    	public Person lookup(String name) {
     
            if (head == null) { 
                return null;
            }
     
            Node current = head;  
            while (current != null) {
                if (current.person.name.compareTo(name)==0) { 
                    return current.person;
                }
                current = current.next; 
            }
            return null;
        }
    public String toString() {
        return String.format("%-10s, %-20s, %-10s", name, address, phoneNr);
      }
     
    }
    }

    Now when I do this I get an compile error saying it cannot find these 3 parameters (name,address,phonenr). How do I implement this method?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: toString method()

    compile error saying it cannot find these 3 parameters (name,address,phonenr)
    What value(s) do you want to be in the String returned by the toString method?
    What are the names of the variables with those values and how can the toString method get their values?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: toString method()

    Hey,just want to say that I have solved this issue with an StringBuilder. (the professor gave an us some hints and examples).But for some reason I couldn't log on to the site,better said "reach" the site till today.

Similar Threads

  1. toString method
    By pelane in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 23rd, 2012, 01:42 AM
  2. toString method
    By feldmanb700 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 17th, 2011, 09:20 PM
  3. Help with toString method and an addObject method?
    By Camisado in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 12th, 2011, 07:00 AM
  4. Valid toString method?
    By dcshoecousa in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 4th, 2010, 11:55 AM
  5. [SOLVED] toString() method
    By chronoz13 in forum Object Oriented Programming
    Replies: 12
    Last Post: January 19th, 2010, 06:44 AM