Okay, well, I am stuck in my programming and really need help to get out of it. :chomp: . I am writing a program that
-builds a binarysearchtree from a file, seperating the words by using scanner class
-allows the user to find/delete/add an occurence of the word in the BST (therefore each word must have an occurence parameter)
-prints out an inorder traversal of the tree into a file
I am using three classes to achieve this, a Word class that implements Comparable<T>, using an overwritten compareTo method, as well as some other basic methods.
The second class is the BST class, that should handle everything involving the BST.
The third is the actual driver.
Here are the 3 classes, and the subsequent problems that I am having...any help is GREATLY appreciated. I am open for chat via AIM if that is preferable, I just need some assistance on understanding this. Thank you,
Word class:
Code:
public class Word<T> implements Comparable<T>
{
String word1;
int occurences1;
public Word()
{
word1 = null;
occurences1 = 0;
}
public Word(String word)
{
word1 = word;
}
public int returnOcc()
{
return occurences1;
}
public String returnWord()
{
return word1;
}
public int incrementCount(int count)
{
occurences1 = count++;
return occurences1;
}
public int compareTo(T anotherWord)
{
try //the try/catch method idea was originated from Lehmann's program sent through E-mail
{
Word compareWord = (Word)anotherWord;//typecast parameter to be of type Word
return word1.compareToIgnoreCase(compareWord.word1); //id is a String, so call the compareTo() for Strings
}
catch (ClassCastException e) //in case parameter was not of type Word
{
System.out.println("In Word class in compareTo method: " + e.getMessage( ));
return Integer.MAX_VALUE; //from the Integer wrapper class, see api documentation
}
}
//The remove punctuation method. I couldn't find the isPunct() method in the javadocs, but I did fine
//that the chars have values to them, so this should strip punctuation from the words, returning the StringBuffer
//that has appended the alphabetic letters to the SB, returning the result without punctuation :D
public String removePunct(String inputString)
{
StringBuffer sb = new StringBuffer();
for (int i = 0; i < inputString.length(); i++)
{
if ((inputString.charAt(i) >= 65 && inputString.charAt(i) <= 90) || (inputString.charAt(i) >= 97 && inputString.charAt(i) <= 122))
{
sb = sb.append(inputString.charAt(i));
}
}
return sb.toString();
}
}
BST class:
Code:
import java.*;
public class BinaryTree<T> extends Word<T>
{
private Node root;
//to create a simple Node class
private static class Node<T>
{
Node left;
Node right;
T data;
Node(T element)
{
left = null;
right = null;
data = element;
}
}
public void BinaryTree()
{
root = null;
}
//allows for 2 lookup methods, one calling the other
public boolean lookup(T data)
{
return(lookup(root, data));
}
//lookup is a method allowing the user to look up a specific node
//needs to be changed to allow for String lookup
private boolean lookup(Node node, T data)
{
if (node==null)
{
return(false);
}
if ((Comparable)data.compareTo((Comparable)node.data) == 0)
{
return(true);
}
else
if ((Comparable)data.compareTo((Comparable)node.data) < 0)
{
return(lookup(node.left, data));
}
else
{
return(lookup(node.right, data));
}
}
//simply inserts a node
public void insert(T data)
{
root = insert(root, data);
}
//finds where the word should go, then inserts it
private Node insert(Node node, T data)
{
if (node==null)
{
node = new Node(data);
}
else
{
if ((Comparable)data.compareTo((Comparable)node.data)<0)
{
node.left = insert(node.left, data);
}
else
{
node.right = insert(node.right, data);
}
}
return(node);
}
//method to simply println, later to be changed into printing into a file
public void printTree()
{
printTree(root);
System.out.println();
}
private void printTree(Node node)
{
if (node == null)
{
return;
}
printTree(node.left);
System.out.print(node.data + " ");
printTree(node.right);
}
}
Driver class:
[code]
import java.*;
//Creates a menu as for what the user wants to do (needs to be created)
//Reads in from a file, creates a BST from those words, removing punct and seperating words
//need to seperate words
//writes an inorder traversal of the BST
public class Driver
Code:
{
public static void main(String args[]) throws FileNotFoundException, IOException
{
}
//I don't understand how to implement either a stringtokenizer
//or a scanner class here...If anyone could help that would be great
public static String getFile(File inwords)
{
StringBuffer insideFile = new StringBuffer();
BufferedReader input = null;
try
{
input = new BufferedReader(new FileReader(inwords));
String line = null;
while((line = input.readLine()) !=null)
{
insideFile.append(line);
insideFile.append("***new line***/n");
}
input.close();
}
catch(FileNotFoundException ex)
{
ex.printStackTrace();
}
catch(IOException ex)
{
ex.printStackTrace();
}
return insideFile.toString();
}
public static void writeFile() throws FileNotFoundException, IOException
//I am curious as to how I could go for writing the BST from here
//Should I put this method in the BST method?
{
Writer output = null;
output = new BufferedWriter(new FileWriter("outputFile"));
output.write("text goes here");
}
}
I also really need help in interweaving the Driver/Word/BST, as I seem to be having a problem linking them to work together.
Again, I need guidance, and I am open to chat over AIM or something similar, as well as through here. Thank you for your time,
Cheers.
p.s....here are my frustrating errors from BST..
Code:
C:\Documents and Settings\xAzraelx\Desktop\School\06_Fall\ITCS2214\Programming Assignments\Programming Assignment 3\Working on\BinaryTree.java:52: cannot find symbol
symbol : method compareTo(java.lang.Comparable)
location: class java.lang.Object
if ((Comparable)data.compareTo((Comparable)node.data) == 0)
^
C:\Documents and Settings\xAzraelx\Desktop\School\06_Fall\ITCS2214\Programming Assignments\Programming Assignment 3\Working on\BinaryTree.java:52: incomparable types: java.lang.Comparable and int
if ((Comparable)data.compareTo((Comparable)node.data) == 0)
^
C:\Documents and Settings\xAzraelx\Desktop\School\06_Fall\ITCS2214\Programming Assignments\Programming Assignment 3\Working on\BinaryTree.java:57: cannot find symbol
symbol : method compareTo(java.lang.Comparable)
location: class java.lang.Object
if ((Comparable)data.compareTo((Comparable)node.data) < 0)
^
C:\Documents and Settings\xAzraelx\Desktop\School\06_Fall\ITCS2214\Programming Assignments\Programming Assignment 3\Working on\BinaryTree.java:57: operator < cannot be applied to java.lang.Comparable,int
if ((Comparable)data.compareTo((Comparable)node.data) < 0)
^
C:\Documents and Settings\xAzraelx\Desktop\School\06_Fall\ITCS2214\Programming Assignments\Programming Assignment 3\Working on\BinaryTree.java:80: cannot find symbol
symbol : method compareTo(java.lang.Comparable)
location: class java.lang.Object
if ((Comparable)data.compareTo((Comparable)node.data)<0)
^
C:\Documents and Settings\xAzraelx\Desktop\School\06_Fall\ITCS2214\Programming Assignments\Programming Assignment 3\Working on\BinaryTree.java:80: operator < cannot be applied to java.lang.Comparable,int
if ((Comparable)data.compareTo((Comparable)node.data)<0)
^
Note: C:\Documents and Settings\xAzraelx\Desktop\School\06_Fall\ITCS2214\Programming Assignments\Programming Assignment 3\Working on\BinaryTree.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
6 errors
Tool completed with exit code 1