0

Hello, I'm getting quite flustered with this program and the errors are starting to get harder to debug. This is my tree.java file:

package TreePkg;
import java.io.FileNotFoundException;
import java.lang.SecurityException;
import java.util.Scanner;
import java.util.NoSuchElementException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.Serializable;
import java.io.EOFException;

class TreeNode<T extends Comparable <T>>
{
        T                       data;
        TreeNode<T>     leftNode;
        TreeNode<T>     rightNode;

        public TreeNode (T item){
                data = item;
                leftNode=rightNode=null;
        }
}

public class Tree <T extends Comparable <T> & Serializable>
{
        private TreeNode<T> root;
        Scanner input = new Scanner(System.in);
        public void Tree (){
                root=null;
        }

        public void insertItem (T item){
                if( root == null )
                        root = new TreeNode<T>( item );
                else
                        insert(root, item);
        }

        private void insert (TreeNode<T> tree, T item){
                if(item.compareTo(tree.data)<0)
                {
                        if(tree.leftNode==null)
                                tree.leftNode = new TreeNode<T>(item);
                        else
                                insert(tree.leftNode, item);
                }
                else if(item.compareTo(tree.data)>0)
                {
 if(tree.rightNode==null)
                                tree.rightNode = new TreeNode<T>(item);
                        else
                                insert(tree.rightNode, item);
                }
        }

        public void deleteItem (T item){
                root = deleteNode(root, item);
        }

        private TreeNode<T> deleteNode (TreeNode<T> tree, T item){
                if(tree != null){
                        if(item.compareTo(tree.data)<0)
                                tree.leftNode=deleteNode(tree.leftNode,item);
                        else if(item.compareTo(tree.data)>0)
                                tree.rightNode=deleteNode(tree.rightNode,item);
                        else if(item.compareTo(tree.data)==0)
                                if(tree.leftNode==null){
                                        tree=tree.rightNode;
                                }
                                else if(tree.rightNode==null){
                                        tree=tree.leftNode;
                                }
                                else{
                                        TreeNode<T> tempNode=tree.leftNode;
                                        while(tempNode.rightNode !=null)
                                                tempNode=tempNode.rightNode;

                                        tree.data=tempNode.data;
                                        tree.leftNode=deleteNode(tree.leftNode,tempNode.data);
                                }
                }
                return tree;
        }
////////preOrder///
        public void preOrderTraversal(){
                if(root !=null)
                        preOrderHelper(root);
                else
                        System.out.println("\nTree is Empty\n");
        }
        private void preOrderHelper(TreeNode<T> tree){
                if(tree==null)
                        return;
                System.out.printf("%s",tree.data);
                preOrderHelper(tree.leftNode);
                preOrderHelper(tree.rightNode);
        }
////////inOrder////
        
  public void inOrderTraversal(){
                if(root != null)
                        inOrderHelper(root);
                else
                        System.out.println("\nTree is Empty\n");
        }
        private void inOrderHelper(TreeNode<T> tree){
                if(tree==null)
                        return;
                inOrderHelper(tree.leftNode);
                System.out.printf("%s", tree.data);
                inOrderHelper(tree.rightNode);
        }
////////postOrder//
        public void postOrderTraversal(){
                if(root !=null)
                        postOrderHelper(root);
                else
                        System.out.println("\nTree is Empty\n");
        }
        private void postOrderHelper(TreeNode<T> tree){
                if(tree==null)
                        return;
                postOrderHelper(tree.leftNode);
                postOrderHelper(tree.rightNode);
                System.out.printf("%s", tree.data);
        }

//      public boolean treeFormatTraversal()
//      private boolean treeForamtHelper(TreeNode<T> tree, String indentSpaces)

        public int lengthIs(){
                return countNodes(root);
        }

        private int countNodes(TreeNode<T> tree){
                if(tree==null)
                        return 0;
                else
                        return countNodes(tree.leftNode)+countNodes(tree.rightNode)+1;
        }

        public boolean findItem(T item){
                TreeNode<T> tree = root;
                boolean found = false;

                while(tree != null && found == false){
                        if(item.compareTo(tree.data)<0)
                                tree = tree.leftNode;
                        else if(item.compareTo(tree.data)>0)
         tree = tree.rightNode;
                        else
                                found=true;
                }
                return found;
        }

        public boolean isEmpty(){
                if(root==null)
                        return true;
                else
                        return false;
        }

        public void clear(){
                root=null;
        }

        public boolean saveToFile(){
                String name;
                System.out.print("Name of the output file: ");
                name = input.nextLine();

                try{
                        ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(name));
                        if(output==null){
                                System.out.println("\nOutput file did not open.\n");
                        }else{
                                output.close();
                        }
                        TreeNode<T> tree;
                        saveTree(output,tree);
                }
                catch(FileNotFoundException fnfe){
                        System.out.println("File not found.");
                        return false;
                }
                catch(SecurityException se){
                        System.out.println("No write access to this file.");
                        return false;
                }
                catch(Exception e){
                        System.out.println("Error opening file.");
                        return false;
                }
                return true;
        }

        private boolean saveTree(ObjectOutputStream output, TreeNode<T> tree){
                output.writeObject(tree.data);
                output.writeObject(tree.leftNode);
                output.writeObject(tree.rightNode);

        }

        public boolean buildFromFile(){
                String name;
                boolean end = false;

                System.out.print("Name of the input file: ");
                name = input.nextLine();

                ObjectInputStream ois = new ObjectInputStream(new FileInputStream(name));

                try{
                        if(input==null){
                                System.out.println("\nInput file did not open.\n");
                        }
                        else if(input !=null){
                                ois.close();
                        }

                }
                catch(FileNotFoundException fnfe){
                        System.out.println("File not found.");
                        return false;
                }
                catch(NoSuchElementException nsee){
                        System.out.println("Invalid input.  Please try again");
                        return false;
                }
                catch(IllegalStateException ise){
                        System.out.println("Illegal state.");
                        return false;
                }
                catch(EOFException eofe){
                        System.out.println("End of file.");
                        end=true;
                        return false;
                }
                catch(Exception e){
                        System.out.println("Error opening file.");
                        return false;
                }
                return true;
                while(end==false){
                                System.out.println("Insert a value from the file.");
                                T obj = (T)ois.readObject();
                                insertItem(obj);
                }
        }
}

The errors I've been getting are relatively similar:
Unreported exception java.io.IOException; must be caught or declared to be thrown
(all lines of the saveTree method)

Unreachable while statement
(end of program)

and then it says the ObjectOutputStream ois = new ObjectOutputStream(new FileInputStream(name)); is an unreported exception java.io.FileNotFoundException AND IOException...

I have no idea what this means as I have included try catch blocks in the appropriate methods.

PLEASE HALP!

Thanks,
Zach

2
Contributors
1
Reply
3
Views
6 Years
Discussion Span
Last Post by JamesCherrill
0

java.io.IOException; must be caught or declared to be thrown

Means what it says. All the statements relating to the IO must be in a try block with a catch that catches IOException

Unreachable while statement

Means what it says. The previous statement is a return, so there's no way for your code to reach the following while.

new ObjectOutputStream(new FileInputStream(name))

This can throw FileNotFoundException or IOException so you must put that statement in a try block with catches that catch FileNotFoundException and IOException. Althouigh you have loits of catches, the statement in question is outside the try.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.