Traverse a Tree via input file

getting error messages that I do not understand

Page 1 of 1

3 Replies - 2272 Views - Last Post: 30 September 2010 - 09:13 AM Rate Topic: -----

#1 ss_hat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 01-March 09

Traverse a Tree via input file

Posted 29 September 2010 - 07:32 PM

Here is the problem I have been asked to solve:
Write a Java program that takes as input text from a file. Build a binary tree of all words in the file. Words are separated by the usual punctuation marks and spaces. From each binary tree node, link the line numbers on which the word is found. The first line in the file is line 1. If a word occurs more than once on a line, do not include the same line number twice. Your program should not be case-sensitive. That is, upper-case and lower-case words are the same. Your program should not be dependent upon the size or content of the file. A test file of 50,000 words should work as well as the test data provided. The output is a sorted cross-reference of all words in the file, along with a list of the lines on which it is found. Your program should ask the user for the file name and handle exceptions appropriately. A test data file for this assignment is attached.

Please write your own binary tree class rather than using one of the Java classes, although you may use code from the book that you adapt. Part of our purpose here is to learn the mechanics of data structures.

For example, for the output might look like this:

a 1 3
all 1 5
are 1 5
as 1
...


Here's my code so far:
import java.util.*;
import java.io.*;

public class DigData
{
      public static void main(String[] args) throws IOException
      {
            int lineNum = 0;
           
            DigData DigData = new DigData();
           
            Scanner file = new Scanner(new File("datafile.txt"));
           
            while(file.hasNextLine())
            {
                  Scanner chopper = new Scanner(file.nextLine());
                 
                  lineNum++;
                 
                  while(chopper.hasNext())
                  {
                        DigData.add(chopper.next().toLowerCase(), lineNum);
                  }
            }
           
            DigData.traverse();
      }
     
      class TreeNode
      {
            private TreeNode left, right;
            private String word;
            private TreeSet<Integer> lines;
           
            public TreeNode(String word)
            {
                  this.word = word;
                  lines = new TreeSet<Integer>();
            }
           
            public String toString()
            {
                  String output = "";
                 
                  output += word+" ";
                 
                  for(int i : lines)
                  {
                        output += i + " ";
                  }
                 
                  return output;
            }
      }
     
      private TreeNode root;
     
      public DigData()
      {
           
      }
     
      public void add(String word, int line)
      {
            if(root == null)
            {
                  root = new TreeNode(word);
                  root.lines.add(line);
            }
            else
            {
                  TreeNode curr = root;
                 
                  while(curr != null)
                  {
                        if(word.equals(curr.word))
                        {
                              curr.lines.add(line);
                              break;
                        }
                        else if(word.compareTo(curr.word) < 0)
                        {
                              if(curr.left != null)
                              {
                                    curr = curr.left;
                              }
                              else
                              {
                                    curr.left = new TreeNode(word);
                                    curr.left.lines.add(line);
                                    break;
                              }
                        }
                        else
                        {
                              if(curr.right != null)
                              {
                                    curr = curr.right;
                              }
                              else
                              {
                                    curr.right = new TreeNode(word);
                                    curr.right.lines.add(line);
                                    break;
                              }
                        }
                  }
            }
      }
     
      public void traverse()
      {
            traverse(root);
      }
     
      public void traverse(TreeNode root)
      {
            if(root == null)
            {
                  return;
            }
           
            traverse(root.left);
            System.out.println(root);
            traverse(root.right);
      }
}



Here is the error I am getting that I'm not quite sure of why? Might just be tired and missing a quote somewhere or something. I have the file in the root direct of the jar/class file so there is no question as to where it is.


Quote

at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at DigData.main(DigData.java:12)

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Traverse a Tree via input file

#2 toshiro  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 21
  • View blog
  • Posts: 137
  • Joined: 27-June 09

Re: Traverse a Tree via input file

Posted 29 September 2010 - 09:17 PM

There is no DigData constructor - compiler is saying, wait, I see that a DigData object was initialized - and the compiler is now looking for a constructor. But there is none to be found.
 DigData DigData = new DigData();




AS well - you have nested the treenode class inside of the digdata class. check your opening and closing curly braces. I recommend seperate .java files for each major class and keep them all in the same package.

File I/O pitfalls - with your current setup, the file needs to be in the exact dame level in the directoryTree such that it exists @ ~/My Documents/someFolder/myJavaApp/

obviously im making up a directory location bt you get the idea.

A nit picky thing that will help with debugging later - put the main method in a serate Driver.class/.java file.
Was This Post Helpful? 0
  • +
  • -

#3 ss_hat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 01-March 09

Re: Traverse a Tree via input file

Posted 30 September 2010 - 08:28 AM

View Posttoshiro, on 29 September 2010 - 08:17 PM, said:

There is no DigData constructor - compiler is saying, wait, I see that a DigData object was initialized - and the compiler is now looking for a constructor. But there is none to be found.
 DigData DigData = new DigData();




AS well - you have nested the treenode class inside of the digdata class. check your opening and closing curly braces. I recommend seperate .java files for each major class and keep them all in the same package.

File I/O pitfalls - with your current setup, the file needs to be in the exact dame level in the directoryTree such that it exists @ ~/My Documents/someFolder/myJavaApp/

obviously im making up a directory location bt you get the idea.

A nit picky thing that will help with debugging later - put the main method in a serate Driver.class/.java file.



Thanks, I got lazy and put it all in one class file. I'll take your advise and see what I can come up with. I'll come back w/any problems or if I manage a solution I'll post it as well. Thank you so much for the reply, my brain was dead yesterday and just couldn't focus anymore lol.
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5805
  • View blog
  • Posts: 12,643
  • Joined: 16-October 07

Re: Traverse a Tree via input file

Posted 30 September 2010 - 09:13 AM

While this is legal, it's hazardous: DigData DigData = new DigData();. Now, am I calling an object or a class method later on?

Don't be so public with your methods or busy in your main. Other than that, it looks fine.

I'd lay it out like so:
import java.util.*;
import java.io.*;

class TreeNode {
	public TreeNode left, right;
	public String word;
	public TreeSet<Integer> lines;
  
	public TreeNode(String word) {
		this.word = word;
		lines = new TreeSet<Integer>();
	}
  
	public String toString() {
		String output = word+" ";
		for(int i : lines) {
			output += i + " ";
		}
		return output;
	}
}


public class DigData {
	private TreeNode root;
	private int lineNum = 0;

	public DigData() { }

	public DigData(String filename) throws IOException {
		Scanner file = new Scanner(new File(filename));
		
		while(file.hasNextLine()) {
			addLine(file.nextLine());
		}
	}
	
	public void addLine(String line) {
		lineNum++;
		Scanner chopper = new Scanner(line);
		while(chopper.hasNext()) {
			add(chopper.next().toLowerCase(), lineNum);
		}
	}

	private void add(String word, int line) {
		if(root == null) {
			root = new TreeNode(word);
			root.lines.add(line);
		} else {
			TreeNode curr = root;
			while(curr != null) {
				if(word.equals(curr.word)) {
					curr.lines.add(line);
					break;
				} else if(word.compareTo(curr.word) < 0) {
					if(curr.left != null) {
						curr = curr.left;
					} else {
						curr.left = new TreeNode(word);
						curr.left.lines.add(line);
						break;
					}
				} else {
					if(curr.right != null) {
						curr = curr.right;
					} else {
						curr.right = new TreeNode(word);
						curr.right.lines.add(line);
						break;
					}
				}
			}
		}
	}
	  
	public void traverse() { traverse(root); }
  
	private void traverse(TreeNode root) {
		if(root != null) {
			traverse(root.left);
			System.out.println(root);
			traverse(root.right);
		}
	}
	
	public static void main(String[] args) throws IOException {
		DigData pgm = new DigData("datafile.txt");
		pgm.traverse();
	}
	
}



Now you may call it with any data file easily or just test it by adding a few lines.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1