I have an Instance I cannot change, a class I could but might be better not to, and the main class that actually runs the program.
At certain parts in the code there is an error between my LinkedBinaryTreeNode.java and java.lang.string, or I guess the String type. these errors can be found on the commented lines.
I don't know how to fix it or really what it actually means as I am a programming novice. Any help is appreciated.

import java.util.List;


 public interface BinaryTreeNode<E> {


    public E getData();


    public void setData(E data);


    public BinaryTreeNode<E> getRoot();


    public BinaryTreeNode<E> getParent();


    public BinaryTreeNode<E> getLeft();


    public void setLeft(BinaryTreeNode<E> child);


    public BinaryTreeNode<E> getRight();


    public void setRight(BinaryTreeNode<E> child);


    public boolean isParent( );


    public boolean isLeaf( );


    public boolean hasLeftChild( );


    public boolean hasRightChild( );


    public int getDepth( );


    public int getHeight( );


    public int size( );


    public void removeFromParent();


    public List<BinaryTreeNode<E>> pathTo( BinaryTreeNode<E> descendant );


    public List<BinaryTreeNode<E>> pathFrom( BinaryTreeNode<E> ancestor );


    public void traversePreorder(Visitor visitor);


    public void traversePostorder(Visitor visitor);


    public void traverseInorder(Visitor visitor);


    public interface Visitor {
       public void visit(BinaryTreeNode node);
    }
}
import java.util.List;
import java.util.Stack;

public class LinkedBinaryTreeNode<E> implements BinaryTreeNode<E> {
    E data;
    BinaryTreeNode<E> parent;
    BinaryTreeNode<E> leftChild;
    BinaryTreeNode<E> rightChild;
    int depth = 0;


    @Override
    public E getData() {
        return data;
    }


    @Override
    public void setData(E data) {
        this.data = data;
    }


    @Override
    public BinaryTreeNode<E> getRoot() {
        BinaryTreeNode<E> currentNode = this;
        if ( currentNode.getParent() != null ) {
            currentNode.getParent().getRoot();
        }
        return currentNode;
    }


    @Override
    public BinaryTreeNode<E> getParent() {
        return parent;
    }

    private void setParent( BinaryTreeNode<E> parent ) {
        this.parent = parent;
    }

    @Override
    public BinaryTreeNode<E> getLeft() {
        return leftChild;
    }


    @Override
    public void setLeft(BinaryTreeNode<E> child) {
        leftChild = child;
        if ( child != null ) {
            ((LinkedBinaryTreeNode<E>) child).setParent(this);
            ((LinkedBinaryTreeNode<E>) child).depth = depth + 1;
        }
    }


    @Override
    public BinaryTreeNode<E> getRight() {
        return rightChild;
    }


    @Override
    public void setRight(BinaryTreeNode<E> child) {
        rightChild = child;
        if ( child != null ) {
            ((LinkedBinaryTreeNode<E>) child).setParent(this);
            ((LinkedBinaryTreeNode<E>) child).depth = depth + 1;
        }
    }


    @Override
    public boolean isParent() {
        return hasLeftChild() || hasRightChild();
    }


    @Override
    public boolean isLeaf() {
        return !isParent();
    }

    @Override
    public boolean hasLeftChild() {
        return leftChild != null;
    }


    @Override
    public boolean hasRightChild() {
        return rightChild != null;
    }


    @Override
    public int getDepth() {
//        int depth = 0;
//        BinaryTreeNode<E> currentNode = this;
//        while ( currentNode.getParent() != null ) {
//            currentNode = currentNode.getParent();
//            depth++;
//        }
        return depth;
    }


    @Override
    public int getHeight() {
        final int[] height = {0};
        BinaryTreeNode<E> root = getRoot();
        root.traversePreorder( node -> {
            int depth = node.getDepth();
            if ( height[0] < depth ) {
                height[0] = depth;
            }
        } );
        return height[0];
    }


    @Override
    public int size() {
        final int[] size = {0};
        traversePreorder( node -> {
            size[0]++;
        });
        return size[0];
    }


    @Override
    public void removeFromParent() {
        if ( getParent() != null ) {
            if ( getParent().getLeft() == this ) {
                getParent().setLeft( null );
            } else {
                getParent().setRight( null );
            }
            setParent( null );
        }
    }


    @Override
    public List<BinaryTreeNode<E>> pathTo(BinaryTreeNode<E> descendant) {
        List<BinaryTreeNode<E>> pathFrom = descendant.pathFrom( this );
        Stack<BinaryTreeNode<E>> stack = new Stack<>();
        for( BinaryTreeNode<E> node : pathFrom ) {
            stack.push( node );
        }
        List<BinaryTreeNode<E>> pathTo = new ArrayList<>();
        while( !stack.isEmpty() ) {
            pathTo.add( stack.pop() );
        }
        return pathTo;
    }


    @Override
    public List<BinaryTreeNode<E>> pathFrom(BinaryTreeNode<E> ancestor) {
        BinaryTreeNode<E> currentNode = this;
        List<BinaryTreeNode<E>> pathFrom = new ArrayList<>();
        while( currentNode != null && currentNode != ancestor ) {
            pathFrom.add( currentNode );
            currentNode = currentNode.getParent();
        }
        if ( currentNode == null ) {
            return new ArrayList<>();
        }
        pathFrom.add( currentNode );
        return pathFrom;
    }


    @Override
    public void traversePreorder(Visitor visitor) {
        visitor.visit( this );
        if ( hasLeftChild() ) getLeft().traversePreorder( visitor );
        if ( hasRightChild() ) getRight().traversePreorder( visitor );
    }

    @Override
    public void traversePostorder(Visitor visitor) {
        if ( hasLeftChild() ) getLeft().traversePostorder( visitor );
        if ( hasRightChild() ) getRight().traversePostorder( visitor );
        visitor.visit( this );
    }


    @Override
    public void traverseInorder(Visitor visitor) {
        if ( hasLeftChild() ) getLeft().traverseInorder( visitor );
        visitor.visit( this );
        if ( hasRightChild() ) getRight().traverseInorder( visitor );
    }
}
import javax.swing.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.*;

public class Program4_v2 extends JFrame {   //extending Jframe for visual tree

    private static void write(LinkedBinaryTreeNode<String> root, String path){
        File file=new File(path);
        try (PrintWriter writer=new PrintWriter(file)){
            root.traversePreorder(node -> {
                if(node.getParent()==null){
                    writer.print(node.getData());
                }
                else{
                    writer.print("\n"+node.getData());
                }
            });
        }
        catch (FileNotFoundException e){
            e.printStackTrace();
        }
    }
    public static void add(BinaryTreeNode<String> oldNode, String data) {       //if statements helping to determine if there are more nodes


        BinaryTreeNode<String> newNode = new LinkedBinaryTreeNode<>(data); // failing line


        if (oldNode.hasLeftChild() && oldNode.getLeft().getData().contains("Q:")) {
            add(oldNode.getLeft(), data);
        } else if (oldNode.hasRightChild() && oldNode.hasLeftChild() && oldNode.getRight().getData().contains("Q")) {
            add(oldNode.getRight(), data);
        } else if (!oldNode.hasLeftChild() && oldNode.getData().contains("Q")) {
            oldNode.setLeft(newNode);
        } else if (!oldNode.hasRightChild() && oldNode.getData().contains("Q")) {
            oldNode.setRight(newNode);
        } else {
            boolean finished = false;
            BinaryTreeNode<String> node = oldNode.getParent();
            while (oldNode.getParent().getRight() != node) {
                if (node.getData().contains("A:")) {
                    node = node.getParent();
                } else if (!node.hasRightChild() && node.getData().contains("Q")) {
                    finished = true;
                    node.setRight(newNode);
                    break;
                } else if (node.getRight().getData().contains("A:") || node.getRight() == oldNode) {
                    node = node.getParent();
                } else {
                    finished = true;
                    add(node.getRight(), data);
                    break;
                }
            }
            if (!finished) {        //if there are more nodes, find them
                if (!node.hasRightChild()) {
                    node.setRight(newNode);
                } else {
                    add(node.getRight(), data);
                }
            }
        }
    }
    private static LinkedBinaryTreeNode<String>load(String path){


        LinkedBinaryTreeNode<String>root=new LinkedBinaryTreeNode<>(null); // failing line


        try {
            Scanner file = new Scanner(new File(path));
            String string = file.nextLine();


            root=new LinkedBinaryTreeNode<>(string); // failing line


            while(file.hasNext()){
                string=file.nextLine();
                add(root,string);
            }
        }
        catch (FileNotFoundException e){
            e.printStackTrace();
        }
        return root;
    }
    private static String question(String question){
        System.out.println(question);
        Scanner scanner=new Scanner(System.in);
        String object=scanner.nextLine();
        if (object.contains("?")){
            return "Q:"+object;
        }
        else{
            return "A:"+object;
        }
    }
    private static void question(LinkedBinaryTreeNode<String>node){
        System.out.println(node.getData().substring(2));
        Scanner scanner=new Scanner(System.in);     //scanner in to along for computer to learn
        String string=scanner.next();
        if (string.contains("no")&& node.isLeaf()){
            String object= question("What was the correct answer?");        //printed text for when computer guesses wrong
            String question=question("What is the difference between "+node.getData().substring(2)+" and "+object.substring(2)+"?");
            String answer=question("What is the correct answer?");


            LinkedBinaryTreeNode<String>oldObject=new LinkedBinaryTreeNode<>(node.getData()); // failing line
            LinkedBinaryTreeNode<String>newObject=new LinkedBinaryTreeNode<>(object()); // failing line


            node.setData(question);
            if (answer.contains("yes")){
                node.setRight(newObject);
                node.setLeft(oldObject);
            }
            else{
                node.setRight(newObject);
                node.setLeft(oldObject);
            }
        }
        else if (string.contains("no")){
            question((LinkedBinaryTreeNode)node.getLeft());
        }
        else if (string.contains("yes")&& node.isLeaf()){       //you, or the computer, did it
            System.out.println("Excellent");
        }
        else{
            question((LinkedBinaryTreeNode)node.getRight());
        }
    }
    static JFrame frame;

    public static void main(String[] args) {
        System.out.println("20-Questions Game!");       //launching game
        LinkedBinaryTreeNode<String>root=load(args[0]);
        boolean play=true;
        while(play){
            System.out.println("Play?");        //asking to play
            Scanner scanner=new Scanner(System.in);
            if (scanner.next().equals("no")){       //you dont want to play! abort!
                play=false;
            }
            else{
                question(root);
            }
        }
        write(root,"20questions.data");     //writing game to new directory for future playing
        frame=new JFrame("20 Questions");
        JPanel panel=new TreeDisplayPanel(root);
        frame.add(panel);
        frame.setSize(1600,900);
        frame.show();
    }
}

What should be expected is a twenty-questions game that the program helps you make by sort-of playing. a GUI is also supposed to pop up but should work fine and seems unrelated to the problem

0 Answers