Question

Creating and testing a Java Trie class, and my main class is hanging up for whatever reason, and running the program never terminates.

import java.util.*;

public class Trie<T> {
private  Node<T> root;

private int count;

public Trie(T rootContent){
    root= new Node<T>(rootContent);
    count=0;
}
public void insert(T[] content){
    Node<T> current=root;
    if(content !=null){
        if(content.length==0){
            current.setFlag(true);
        }
        for(int i=0; i<content.length;i++){
            Node<T> child=current.getChild(content[i]);
            if(child!=null){
                current=child;
            }
            else{
                current=current.addChildren(content[i]);
            }
            if(i==content.length-1){
                if(current.isLast()){
                    current.setFlag(true);
                    count++;
                }
            }
        }
    }
}

public boolean search(T[] content){
    Node<T> current=root;
    for(int i=0;i<content.length;i++){
        if(current.getChild(content[i])==null){
            return false;
        }
        else{
            current=current.getChild(content[i]);
        }
    }
    if(current.isLast()){
        return true;
    }
    else{
        return false;
    }

}


public int getCount(){
    return count;
}

public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("number entries: ");
    sb.append(count);
    return sb.toString();
}
private static class Node<T>{
    private T content;

    private boolean endFlag;

    public List<Node<T>> children=new ArrayList<Node<T>>();

    public Node(T content){
        this.content=content;
        this.endFlag=false;
        this.children=new ArrayList<Node<T>>();
    }

    public T getContent(){
        return content;
    }

    public Node<T> addChildren(T content){
        Node<T> t=new Node<T>(content);
        children.add(t);
        return t;
    }

    public Node<T> getChild(T content){
        if(children!=null){
            for(Node<T> t : children){
                if(t.getContent().equals(content)){
                    return t;
                }
            }
        }
        return null;
    }

    public void setFlag(boolean endFlag){
        this.endFlag=endFlag;
    }

    public boolean isLast(){
        return endFlag;
    }

}
public static void main(String[] args) {

        Trie file1=new Trie("");
        Trie file2=new Trie("");
        if(args.length==0 || args[0]==null){
            System.out.println("Please give a valid command file");
        }
        else{
        try{    
        Scanner reader1 = new Scanner(new FileInputStream(args[2]));
        Scanner reader2 = new Scanner(new FileInputStream(args[3]));
        String d=args[0];
        String n=args[1];
        int depth=Integer.parseInt(d);
        int numberOfStrings=Integer.parseInt(n);
        while(reader1.hasNext());{
            String[] ary=reader1.next().split("");
            file1.insert(ary);
        }
        while(reader2.hasNext());{
            String[] ary=reader1.next().split("");
            file2.insert(ary);
        }
        reader1.close();
        reader2.close();
        System.out.print(file1);
        System.out.print(file2);
    }
        catch(Exception e){

    }
}

}
}

I think the insert operation is where things are hangning up, the text files are nothing more than random strings "dog god dog act cata asfdoh aosifdh oawrhi "

Was it helpful?

Solution

Looking at these lines shows the issue:

    while(reader1.hasNext());{
        String[] ary=reader1.next().split("");
        file1.insert(ary);
    }
    while(reader2.hasNext());{
        String[] ary=reader1.next().split("");
        file2.insert(ary);
    }

There should not be a semi-colon after while(reader2.hasNext()) or while(reader1.hasNext()) because this terminates the loop. I'm not actually sure why the semi colon here causes it to hang, but for some reason it does. I'm sure somebody will be able to help us both out here, but removing the semi-colons will fix the issue. It's strange, however, that the code is compilable.

Are you using an IDE? If you are, in the future you should attempt to debug your code within the IDE before asking for help on here. Saves everybody's time, and it's a good (read: necessary) skill to have.

Also for further reference, you have parameterised the type of Trie, but not specified a type when instantiating it.

Trie file1=new Trie("");

should be

Trie<String> file1=new Trie<String>("");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top