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

Thread: Stack Overflow

  1. #1
    Junior Member
    Join Date
    May 2021
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Stack Overflow

    Hey guys, i need to construct text to a linked list and then sort it lexicographically, i used merge sort method for that,
    but it triggers stack overflow when going to my split method , and i can't seem to find what's wrong with the recursion.
    ideas?

    public class TextList {
        WordNode _head;
     
        public TextList () 
        {
            _head = null;    
        }
        public TextList (String text) 
        {
     
            construct(text);
            mergeSort(_head);
        }
        private WordNode construct(String text)
        {
            int i = 0;
            String tmp = "";
            int index = 0;
            if (text != null)
            {
                for (; i < text.length() ; i++ ) 
                {
                    if (text.charAt(i) == ' ')
                    {
                        index++;
                        _head = new WordNode(tmp);
                        _head.setNext(addText(_head, text.substring(index)));
                        return _head;
                    }
                    else // charAt i = a characeter
                    {
                        tmp += text.charAt(i);
                        index++;
                    }
                }  
            }
            _head = null;  
            return _head;
        }
        private WordNode addText(WordNode behind, String text)
        {
            String tmp = "";
            int index = 0;
                while (index < text.length()) 
                {
                    if (text.charAt(index) == ' ')
                    {
                        WordNode tmpo = new WordNode(tmp);
                        behind.setNext(tmpo);
                        index++;
                        behind = tmpo;
                        tmp = "";
                    }
                    else // charAt i = a characeter
                    {
                        tmp += text.charAt(index);
                        index++;
                    }
                }  
            WordNode tmpo = new WordNode(tmp);
            behind.setNext(tmpo);
            return _head;
        }
        private WordNode mergeSort (WordNode node)
        {
            if (node == null || node.getNext() == null)
                return null;
            WordNode list2 = split(node);
            node = mergeSort(node);
            list2 = mergeSort(list2);
            return merge(node, list2);
        }
        private WordNode split(WordNode node)
        {
            if (node == null || node.getNext() == null)
                return null;
            WordNode list2 = node.getNext();
            node.setNext(list2.getNext());
            list2.setNext(split(list2.getNext()));                             <<<<----------- Stack overFlow happens in here
            return list2;
        }
        private WordNode merge(WordNode list1, WordNode list2)
        {
            if(list1 == null)
                return list2;
            if(list2 == null)
                return list1;
            if (list1.getValue().compareTo(list2.getValue()) < 0)
            {
                list1.setNext(merge (list1.getNext(), list2));
                return list1;
            }
            else
            {
                list2.setNext(merge(list1, list2.getNext()));
                return list2;
            }
        }
    public String toString() 
        {
            WordNode temp = _head;
            if (temp == null)
                return "";
            return toString(temp, 1, temp.getValue(), "");
        }
        private String toString(WordNode node, int count, String current, String res)
        {
            if (node == null)
                return res;
            if (node.getNext()  == null)
            {
                current = node.getValue();
                res +=  current + "\t" + count + "\n";
                return res;
            }
            if (node.getValue() == node.getNext().getValue())
            {
                count++;
                return toString(node.getNext(), count, current, res);
            }
            else
            {
                current = node.getValue();
                res +=  current + "\t" + count + "\n";
                    return toString(node.getNext(), 1, current, res);
            }
     
        }
    }

    this is my tester :

     
    public class Tester {
     
        public static void main(String args[]) {
            TextList List0 = new TextList();
            System.out.println(List0);
            TextList List1 = new TextList("hello nice to meet you my name is ");
            System.out.println(List1);
     
    }
     
    }

    I spared you some other code that i wrote that i think that is irrelevant, and there is another class called WordNode to store data of the node
    with getters and setters and somemore.

    Thanks!

  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: Stack Overflow

    Can you copy some of the stack trace and paste it here so we can see what methods were being called that caused the overflow?

    Need definition for the WordNode class to be able to compile for testing.

    The code needs some comments describing what it is trying to do and how it is going to do it.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. what is difference between call stack and stack tace?
    By me_shankara in forum Exceptions
    Replies: 6
    Last Post: October 27th, 2018, 03:23 AM
  2. Recursive method - stack overflow
    By yakir_g in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 8th, 2013, 07:38 AM
  3. [SOLVED] Banker Deadlock detection algorith going haywire and likely a stack overflow error
    By javapenguin in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 23rd, 2012, 05:23 PM
  4. [SOLVED] stack overflow error
    By Goldfinch in forum What's Wrong With My Code?
    Replies: 14
    Last Post: February 3rd, 2012, 01:07 AM
  5. Replies: 0
    Last Post: October 14th, 2008, 06:40 PM