1
public class ContactList {

    private ContactNode head;
    private ContactNode last;
    public ContactNode current;
    public ContactList value;

    public ContactList() {}

    public void addNode(ContactNode input) {
        if (this.head == null) {
            this.head = input;
            this.last = input;
        } else last.setNext(input);
        input.setPrev(last);
        this.last = input;
    }

    public void traverse() {
        System.out.println();
        current = this.head;
        while (current != null) {
            System.out.print(current.getName() + " ");
            System.out.println("");
            current = current.getNext();
        }
        System.out.println();
    }


    public void insertNewFirstNode(String value) {
        ContactNode newNode = new ContactNode(value);
        head = newNode;
        if (last == null) {
            last = head;
        }
    }

    public void sort() {
        ContactList sorted = new ContactList();
        current = head;
        while (current != null) {
            int index = 0;

            if ((current.getName() != null)) {
                index = this.current.getName().compareTo(current.getName());
                if (index == 1) {
                    sorted.insertNewFirstNode(current.getName());
                }
                current = current.getNext();
            } else if ((current != null)) {
                System.out.print(sorted + "\n");
            }
        }
    } // end contactList

Main Method:

import java.util.Scanner;
import java.io.FileReader;
import java.io.FileNotFoundException;

public class ContactMain {
    public static void main(String[] args) {
        try {
            FileReader filepath = new FileReader("data1.txt");
            Scanner k = new Scanner(filepath);
            ContactList myList = new ContactList();
            while (k.hasNextLine()) {
                String i = k.nextLine();
                myList.addNode(new ContactNode(i));
            }

            myList.traverse();
            myList.sort();
            myList.traverse();


        } catch (FileNotFoundException e) {
            System.out.println("File Not Found. ");
        }
    }
}

Node Class:

public class ContactNode {

    private String name;
    public int index;
    private ContactNode prev;
    public ContactNode next;

    ContactNode(String a) {
        name = a;
        index = 0;
        next = null;
        prev = null;
    }

    public ContactNode getNext() {
        return next;
    }

    public ContactNode getPrev() {
        return prev;
    }

    public String getName() {
        return name;
    }

    public int getIndex() {
        return index;
    }

    public void setNext(ContactNode newnext) {
        next = newnext;
    }

    public void setPrev(ContactNode newprevious) {
        prev = newprevious;
    }

    public void setName(String a) {
        name = a;
    }

    public void setIndex(int b) {
        index = b;
    }
}

I am making a program for fun that reads in contact information from a text file and puts them into a Linked List. I want to create a sort() method to sort each node or name alphabetically. I've done a good amount of research and my method only prints code like: ContactList@282c0dbe, by as many lines as my text file.

Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
Raoul Duke
  • 131
  • 7
  • 24

2 Answers2

0

You need custom Comparator for sorting, and to pretty print your List you need to implement toString() in ContactList class

Community
  • 1
  • 1
jmj
  • 237,923
  • 42
  • 401
  • 438
0

what is ContactList@282c0dbe?

It is class name follow by at sign and hash code at the end, hash code of the object.All classes in Java inherit from the Object class, directly or indirectly . The Object class has some basic methods like clone(), toString(), equals(),.. etc. The default toString() method in Object prints “class name @ hash code”.

What is the solution?

You need to override toString method in ContactList class because it is going to give you clear information about the object in readable format that you can understand.

The merit about overriding toString:

Help the programmer for logging and debugging of Java program

Since toString is defined in java.lang.Object and does not give valuable information, so it is good practice to override it for subclasses.

  @override
  public String toString(){
     // I assume name is the only field in class test
     return name + " " + index;
  }

For sorting, you should implement Comparator interface since your object does not have natural ordering. In better sense, if you want to define an external controllable ordering behavior, this can override the default ordering behavior

read more about Comparator interface

Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58