Okay, so I have a rather easy problem that I can't seem to figure out.
I'm getting cannot find symbol errors for T item=(T)input.next();
The program is a test file for a Binary Tree program. I was told the input 'item' has to be in T type. Obviously they have to be in T type to be accepted by the insert, delete, and find methods in the actual tree program. I guess what I'm asking is how to make it so I can declare it as such in the test file.
Tree:
package treePkg;
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;
import java.io.IOException;
import java.io.FileNotFoundException;
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 (){//empty tree
root=null;
}
//INSERT
public void insertItem (T item){//non-recursive insert into tree
if( root == null )
root = new TreeNode<T>( item );
else
insert(root, item);
}
private void insert (TreeNode<T> tree, T item){//Recursive call insert, find position for new 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);
}
}
//END INSERT
//DELETE
public void deleteItem (T item){//non-rec delete item
root = deleteNode(root, item);
}
private TreeNode<T> deleteNode (TreeNode<T> tree, T item){//rec, search tree for item to delete
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;
}
//END DELETE
//ORDERS
//preOrder
public void preOrderTraversal(){//begin
if(root !=null)
preOrderHelper(root);
else
System.out.println("\nTree is Empty\n");
}
private void preOrderHelper(TreeNode<T> tree){//rec, print tree preorder
if(tree==null)
return;
System.out.printf("%s",tree.data);
preOrderHelper(tree.leftNode);
preOrderHelper(tree.rightNode);
}
//inOrder
public void inOrderTraversal(){//begin
if(root != null)
inOrderHelper(root);
else
System.out.println("\nTree is Empty\n");
}
private void inOrderHelper(TreeNode<T> tree){//rec, print tree inorder
if(tree==null)
return;
inOrderHelper(tree.leftNode);
System.out.printf("%s", tree.data);
inOrderHelper(tree.rightNode);
}
//postOrder
public void postOrderTraversal(){//begin
if(root !=null)
postOrderHelper(root);
else
System.out.println("\nTree is Empty\n");
}
private void postOrderHelper(TreeNode<T> tree){//rec, print tree postorder
if(tree==null)
return;
postOrderHelper(tree.leftNode);
postOrderHelper(tree.rightNode);
System.out.printf("%s", tree.data);
}
//END ORDERS
//FORMAT
public void treeFormatTraversal(){//begin
String space =" ";
treeFormatHelper(root,space);
return;
}
private void treeFormatHelper(TreeNode<T> tree, String indentSpaces){
if(tree==null)
return;
if(isEmpty())
System.out.printf("%s",tree.data);
}
//END FORMAT
//LENGTH
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;
}
//END LENGTH
//FIND EMPTY CLEAR
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;
}
//END FIND EMPTY CLEAR
//SAVE
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();
saveTree(output,root);
}
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){
try{
output.writeObject(tree.data);
output.writeObject(tree.leftNode);
output.writeObject(tree.rightNode);
}catch(java.io.IOException exp){exp.printStackTrace();}
return true;
}
//END SAVE
//BUILD
public boolean buildFromFile(){
String name;
boolean end = false;
boolean eo = false;
System.out.print("Name of the input file: ");
name = input.nextLine();
try{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(name));
if(input==null)
System.out.println("\nInput file did not open.\n");
else if(input !=null)
input.close();
while(eo==false)
{
System.out.println("Insert a value from the file.");
T obj = (T)ois.readObject();
insertItem(obj);
}
}
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.");
eo=true;
return false;
}
catch(Exception e){
System.out.println("Error opening file.");
return false;
}
if(eo==true){
end = false;
return end;
}
else{
end = true;
return end;
}
}
//END BUILD
}
Tree Test:
import java.util.Scanner;
import treePkg.Tree;
import menuPkg.Menu;
public class TreeTest
{
public static void main(String[]args){
Scanner input =new Scanner(System.in);
Tree<Integer> tree = new Tree<Integer>();
String[] opts={"Quit","Insert item","Delete item","Find item","Get length of tree","Determine if tree is empty","Clear the tree","Print the tree in sorted order","Print the tree in pre-order","Print the tree in post-order","Print the tree in tree format","Save tree to a file","Build tree from a file"};
Menu menu=new Menu(opts);
int choice=0;
do{
choice=menu.runMenu();
try{
switch(choice){
case 1://insert
System.out.print("Enter a value to insert: ");
T item=(T)input.next();
tree.insertItem(item);
tree.treeFormatTraversal();
break;
case 2://delete
System.out.print("Enter a value to delete: ");
item=(T)input.next();
tree.deleteItem(item);
tree.treeFormatTraversal();
break;
case 3://find
System.out.print("Enter a value to find: ");
item=(T)input.next();
tree.findItem(item);
break;
case 4://length
// System.out.print("Enter a value to find length: ");
// item=(T)input.next();
tree.lengthIs(item);
break;
case 5://empty
if(tree.isEmpty())
System.out.println("Tree is empty");
else
System.out.println("Tree is not empty");
break;
case 6://clear
tree.clear();
System.out.print("Tree cleared");
break;
case 7://inorder
tree.inOrderTraversal();
break;
case 8://preorder
tree.preOrderTraversal();
break;
case 9://postorder
tree.postOrderTraversal();
break;
case 10://tree
tree.treeFormatTraversal();
break;
case 11://save
tree.saveTree();
break;
case 12://build
tree.saveToFile();
break;
case 0://quit
System.out.print("Thank you for playing");
break;
}
}
catch(InputMismatchException eme){
System.out.print("Invalid input.");
}
}while(choice != 0);
}
}
Thanks