Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Nov 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Java - Program need help with BST, Comparables, Scanner (java5).

    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

  • #2
    Gox
    Gox is offline
    Regular Coder Gox's Avatar
    Join Date
    May 2006
    Location
    Ontario, Canada
    Posts
    392
    Thanks
    2
    Thanked 20 Times in 20 Posts
    My guess from the looks of it is a Casting issue.

    if ((Comparable)data.compareTo((Comparable)node.data) < 0)

    That line to me if we were to read it in English would say:
    take data, compare it to some comparable object. Then cast the result to type Comparable and check it's less than 0.

    I'm betting whatever type data is doesn't have a comparTo method.

    It could be as simple as moving one bracket, but it's a guess and I'll let you test it. Try,

    if ((Comparable data).compareTo((Comparable)node.data) < 0)
    OR
    if (((Comparable) data).compareTo((Comparable)node.data) < 0)

    Or something like that. As I pointed out, I think your first (Comparable) cast is casting the result of data.compareTo rather than casting data to Comparable and then calling compareTo.

    I'm not sure if that made any sense at all, I'm without sleep.

    Good luck.

  • #3
    New to the CF scene
    Join Date
    Nov 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you, VERY much. I've been puzzling over those for a couple days, and your second method worked. (The (Comparable)Data instead of the Comparable Data.)

    Now I just gotta figure out the rest, but at least I can test it now....I'm sure more questions might come...thanks!

  • #4
    New to the CF scene
    Join Date
    Nov 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Can someone guide me on how to implement the BinaryTree.java into my driver?

    As in, how to create a new BinaryTree and then set it up...

    Here are my ideas:

    Create a new BinaryTree
    Use a while method, while scanner has next
    then access BinaryTree function insert with scanner.next()

    That way, I'm pretty sure it will order the BinaryTree as it sifts through the words chosen by the scanner....I just don't know how to set up the BinaryTree from my driver.

    I tried the ordinary BinaryTree BT = new BinaryTree();
    but it didn't work...
    Code:
    C:\Documents and Settings\xAzraelx\Desktop\School\06_Fall\ITCS2214\Programming Assignments\Programming Assignment 3\Working on\Driver.java:11: invalid method declaration; return type required
    	BinaryTree() BT = new BinaryTree();
            ^
    C:\Documents and Settings\xAzraelx\Desktop\School\06_Fall\ITCS2214\Programming Assignments\Programming Assignment 3\Working on\Driver.java:11: ';' expected
    	BinaryTree() BT = new BinaryTree();
                         ^
    2 errors
    
    Tool completed with exit code 1
    I also got the driver working as it should, it prints to either a file or dos window, removing punctuation as it goes, so that is good news. I am pretty sure this is the last big step I need to take...ANY advice would be great, thanks.

    Cheers,


    edit:
    Okay, after reviewing it, I am pretty sure it's because I never set up the BinaryTree.

    I have a method called public void BinaryTree()
    in which it sets the root to null, but that doesn't actually set it up, does it?

    thanks for any comments.

    Edit 2 : thank god for dumb mistakes...it's doing well now ^^
    Last edited by xazraelx; 11-03-2006 at 06:16 PM.

  • #5
    Gox
    Gox is offline
    Regular Coder Gox's Avatar
    Join Date
    May 2006
    Location
    Ontario, Canada
    Posts
    392
    Thanks
    2
    Thanked 20 Times in 20 Posts
    I only had a quick read of your message, but it looks like your Binary Tree constructor is wrong...

    if, public void BinaryTree(){...} is suppose to be the constructor, then change it to

    public BinaryTree(){...}

    The first definition is for methods not constructors. Constructors make objects, methods access/modify/compute etc data within objects.
    Last edited by Gox; 11-03-2006 at 07:15 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •