Hi, I have this little program with two classes in it. The first class Kontakt (Contact in english) contains information about a person (name,surname and number). Now, in the PhoneBook class I am trying to implement a LinkedList <Kontakt>, i.e. a list of contacts. Now, all the methods work fine, but when I try to print it out, I get weird strings, something like addresses of the objects. I really do not know where the problem. Here is my code. Thank you for any help in advance.
The first class Kontakt.

public class Kontakt {

	String name,surname;
	int number;
	
	public Kontakt (String name, String surname, int telBroj){
		this.name = name;
		this.surname = surname;
		this.number = number;
	}

	public String getName() {
		return name;
	}

	public String getSurname() {
		return surname;
	}

	public int getNumber() {
		return number;
	}

	
	
	
}

And the second class PhoneBook.

import java.util.*;

/*class Jazol {
	
	 Kontakt element;
	 Jazol succ;
	
	public  Jazol(Kontakt element, Jazol succ) {
		this.element = element;
		this.succ = succ;
	}

}*/


public class PhoneBook {
	
	private LinkedList<Kontakt> list;
	
	public PhoneBook(){
		list = new LinkedList<Kontakt>();
	}
	
	
	
	
	
	
	
	public static void main(String [] args) {
		
		Iterator iterator;
		Kontakt robert = new Kontakt("Robert","Gliguroski",12);
		Kontakt bozidar = new Kontakt("Bozidar","Menkinoski",13);
		Kontakt pepi = new Kontakt("Pepi","Bogdanoski",14);
		
		LinkedList<Kontakt> list = new LinkedList<Kontakt>();
		list.addFirst(robert);
		list.add(bozidar);
		
		int size = list.size();
		
		iterator = list.iterator();
		while(iterator.hasNext()){
			System.out.print(iterator.next() + " ");
		}
		System.out.println();
		
		System.out.println("List's length " + size);
		
		
		iterator = list.iterator();
		while(iterator.hasNext()){
			System.out.print(iterator.next() + " ");
		}
		
		
		
		
	}
}
System.out.print(iterator.next() + " ");

The above code is your problem. Here you are printing the object and not its contents, hence the reason for getting those strange values in your output.
The reason is because you see the above code is exactly like :-

System.out.print(iterator.next().toString() + " ");

And by default the toSring() method of the Object (the super class of all objects in Java) class is called which prints that stuff.

What I suggest is that in our Kontakt class override the method "toString()" method and return a properly formatted String which you would displayed whenever anyone made such a call on the Kontakt class.

An example would be like:-

public class Kontakt {
.
.
.
.
  public String toString() {
    return "Name : " + name + " Surname : " + surname + 
        " Number : " + number;
}

Note above code is just for illustration, I haven't compiled it or anything.

BTW use [code=java] [/code] to envelop your code. That way it will also provide syntax highlighting also.

commented: Helpful. +15

Thanks a lot ! I see the mistake now, I keep forgetting about the mighty Object class :) This really helped me. Have a nice day !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.