Results 1 to 4 of 4

Thread: BST problem

  1. #1
    unc123w's Avatar
    unc123w is offline Member
    Join Date
    Sep 2008
    Posts
    18
    Rep Power
    0

    Default BST problem

    The objective for my program is to read a text file, build an index tree object from the words in the file, then display the index by performing an inorder traversal of the tree. So far, I've created a bufferedreader that reads a text file then prints it to a string. How do I get it to read an actual text file, say from my C drive?

    Java Code:
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    import java.util.*;
    
    
    
    class IndexTree<T extends Comparable <? super T>> extends BinarySearchTree<T> {
    	
    	public BinaryNode<T> left;
    	public BinaryNode<T>right;
    	Object value;
    	
    	public IndexTree() {
    	
    	     IndexTree tree = new IndexTree();
    	
    	
    }
    	
        private BinaryNode<T> insert( T x, BinaryNode<T> t )
        {
            if( t == null )
                return new BinaryNode<T>( x, null, null );
    		
    		int compareResult = x.compareTo( t.element );
    			
            if( compareResult < 0 )
                t.left = insert( x, t.left );
            else if( compareResult > 0 )
                t.right = insert( x, t.right );
            else
                ;  // Duplicate; do nothing
            return t;
        }
    	
    	   private void printTree( BinaryNode<T> t )
    	    {
    	        if( t != null )
    	        {
    	            printTree( t.left );
    	            System.out.println( t.element );
    	            printTree( t.right );
    	        }
    	    }
    	
    }
    
    
    class Prog4 {
    	
    		
    		static public String getContents(File aFile) {
    			StringBuilder contents = new StringBuilder();
    		
    	
    		try{
    			BufferedReader keyboard = new BufferedReader (new FileReader (aFile));
    		
    			try {
    			
    			String line = null;
    
    		while ((line = keyboard.readLine()) != null){
    			contents.append(line);
    			contents.append(System.getProperty("line.separator"));
    
    		      }
    		
    			}
    			finally {
    				keyboard.close();
    			}
    			
    		}catch(IOException e){}
    			return contents.toString();
    
    	
    		}
    		
    		public static void main (String[] args) {
    			File testFile = new File("In this program you will develop an IndexTree class that extends the BinarySearchTree class. ");
    			System.out.println("Contents of the file: " + getContents(testFile));
    		}
    	}

  2. #2
    unc123w's Avatar
    unc123w is offline Member
    Join Date
    Sep 2008
    Posts
    18
    Rep Power
    0

    Default

    I changed this line:
    File testFile = new File("In this program you will develop an IndexTree class that extends the BinarySearchTree class. ");
    to
    File testFile = new File("testFile1.txt");

    which is located in the source package of my project

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    If its solved, please mark as solved.

    Look at the FileChooser class if you want the user to pick the file. Do a Search here for coding examples.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by unc123w View Post
    I changed this line:
    File testFile = new File("In this program you will develop an IndexTree class that extends the BinarySearchTree class. ");
    to
    File testFile = new File("testFile1.txt");

    which is located in the source package of my project
    Is that solve your question? If so mark it as solved.

    Cases like this, it's better to use separate folder for locate files. Use absolute path to refer them. That's much clear, much better in implementation practice.

Posting Permissions

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