Results 1 to 4 of 4
Thread: BST problem
- 11-10-2008, 12:16 AM #1
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)); } }
- 11-10-2008, 02:05 AM #2
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
- 11-10-2008, 02:53 AM #3
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.
- 11-10-2008, 09:33 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks