Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8937023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:20:26+00:00 2026-06-15T10:20:26+00:00

I get stack overflow error when I try to load(). I am trying to

  • 0

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;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:20:27+00:00Added an answer on June 15, 2026 at 10:20 am
    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.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Dear Stack Overflow Community, I'm attempting to get the date from a datepicker and
When you reach 1000 reputation on stack overflow you get an expendable usercard when
One obvious way to cause a stack overflow and get Segmentation fault would be
I had posted this earlier on Stack Overflow, but couldn't get a positive result.
Hi stack overflow users. Error description: For a mini project I have decided to
I'm trying to implement voting very similar to Stack Overflow's. There are multiple items
I get the following error Web Service method name is not valid when i
I'm trying to pull a gzipped json file from http://api.discogs.com/release/339901 . I have https://github.com/waveto/node-compress
First, I already read 3 or 4 tutorials, 20 related questions on stack overflow,
I'm having a stack overflow error relating to android dialogs. I have around 21

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.