Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I get stack overflow error when I try to load(). I am trying to read PhoneBookEntry objects from a txt file. The txt file has a name and number which make up the PhoneBookEntry object.

Can you please let me know what I'm doing wrong?

package HashSet;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Scanner;

public class PhoneBook {
    int capacity = 10;
    private ArrayList<PhoneBookEntry>[] buckets;

    public PhoneBook() {
        this(10);
    }

    public PhoneBook(int size) {
        capacity = size;
        buckets = new ArrayList[size];
        for (int i = 0; i < buckets.length; i++) {
            buckets[i] = new ArrayList<PhoneBookEntry>();
        }
    }

    public int getSize() {
        int tot = 0;
        for (ArrayList<PhoneBookEntry> x : buckets)
            tot += x.size();
        return tot;
    }

    public boolean add(PhoneBookEntry entry) {
        if (contains(entry))
            return false;
        int x = Math.abs(entry.hashCode());
        buckets[x % buckets.length].add(entry);
        return true;
    }

    public void load()
    {
        InputStream is = getClass().getClassLoader().getResourceAsStream("phone.txt");
        Scanner scan = new Scanner(is);
        if (scan.hasNext())
            add(new PhoneBookEntry(scan.next());
    }
    public void bucketSize() {
        for (int i = 0; i < buckets.length; i++)
            System.out.println(i + "    " + buckets[i].size());
    }

    public boolean contains(PhoneBookEntry word) {
        int x = Math.abs(word.hashCode());
        return buckets[x % buckets.length].contains(word);
    }

    public int getCapacity() {
        return capacity;
    }

    public static void main(String[] args) {
        PhoneBook phone = new PhoneBook();
        phone.load();
    }
}

package HashSet;

import java.util.LinkedList;
import java.util.ListIterator;

public class PhoneBookEntry {
    String n;
    Integer nr;
    LinkedList<PhoneBookEntry> list;

    public PhoneBookEntry(String name, int number) {
        list = new LinkedList<PhoneBookEntry>();
        n = name;
        nr = number;
        list.add(new PhoneBookEntry(n, nr));
    }

    public String getN() {
        return n;
    }

    public void setN(String n) {
        this.n = n;
    }

    public Integer getNr() {
        return nr;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((n == null) ? 0 : n.hashCode());
        result = prime * result + ((nr == null) ? 0 : nr.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        PhoneBookEntry other = (PhoneBookEntry) obj;
        if (n == null) {
            if (other.n != null)
                return false;
        } else if (!n.equals(other.n))
            return false;
        if (nr == null) {
            if (other.nr != null)
                return false;
        } else if (!nr.equals(other.nr))
            return false;
        return true;
    }

    public void setNr(Integer nr) {
        this.nr = nr;
    }

    @Override
    public String toString() {
        return n + " " + nr;
    }
}
share|improve this question
 
You call new PhoneBookEntry(...) in PhoneBookEntry constructor –  nhahtdh Dec 5 '12 at 1:44
 
oh let me try to fix that constructor –  Prasanth Dec 5 '12 at 1:45

2 Answers

up vote 1 down vote accepted
public PhoneBookEntry(String name, int number) {
    list = new LinkedList<PhoneBookEntry>();
    n = name;
    nr = number;
    list.add(new PhoneBookEntry(n, nr));
}

is causing an infinite recursion. You likely need a new class to put in the linked list (PhoneNumber or some such if a PhoneBookEntry can contain multiple names/numbers, otherwise ditch it.)

share|improve this answer

Every new phone book entry creates a new phone book entry that creates a new phone book entry that creates a new phome book entry, etc... ad infinitum. That is, until the stack space runs out.

You need to rethink your application data structure.

share|improve this answer
 
ok I got it thanks so much for explaining :) really appreciate it –  Prasanth Dec 5 '12 at 1:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.