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 8788929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:15:49+00:00 2026-06-13T22:15:49+00:00

Creating and testing a Java Trie class, and my main class is hanging up

  • 0

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 “

  • 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-13T22:15:50+00:00Added an answer on June 13, 2026 at 10:15 pm

    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>("");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating simple chess board game in java, and they are running smoothly,
I'm new to unit testing using nunit (and Java development in general). When creating
I'm using JDK 7 and testing Play! 1.2.4. I was creating a test class
I am creating an automated testing framework in Perl for regression tests. I would
I'm testing some cookies that I'm creating via JavaScript. Is there a way to
I'm creating an EnityList to do some client side testing with my ViewModel. Something
I am creating a general general mock-client for testing HTTP-interactions. For this, I would
I'm currently creating an iPhone app and we need some testing with about 20~30
I am creating an PhoneGap application for iPhone. When I am designing and testing
Creating a class at runtime is done as follows: klass = Class.new superclass, &block

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.