简体   繁体   中英

add words from file java

如何使用Java从文件中添加单词?

You are only comparing it to one of the children.

for(int i=0; i<rootChildren.length; i++){
                    Node rootChild = rootChildren[i];
                    //see if the addChar1 already exists in the tree
                    //if it doesn't

                    if(!rootChild.equals(addChar1)){
                        //add the addChar1 as a child of the root
                        root.addChild(addChar1);

                    }
                    else{
                        System.out.println(addChar1.getItem() + " Exists in the tree already");
                    } 

Already adds it after it isn't equal to the first child. You want to see if it equals ANY of the children. What you could do is something like this:

  int equalsnrofchildren = 0;
    for(Node rootChild : rootChildren){//loops through all the children
                        //see if the addChar1 already exists in the tree
                        //if it doesn't

                        if(rootChild.equals(addChar1)){
                            equalsnrofchildren++;

                        }
    }

    if(equalsnrofchildren == 0){
    root.addChild(addChar1);
    }else{
System.out.println("already exists..");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM