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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Binary Tree Iterator

    Hi Guys,

    First time poster, so please be kind!

    I am having some trouble with an assignment and would appreciate some help if available.

    We are required to construct a Binary Search Tree (given .class files for implementation). I think I have constructed the tree correctly, however there seems to be a problem with the Iterator (implementation also provided as .class - ie. not part of the util API.)

    Without giving away my whole assignment - following is what I think might be relevant.

    Thanks in advance for your help!

    Code:
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.border.*;
    import java.awt.event.*;
    import java.util.Scanner;
    import java.io.*;
    import ds.util.*;
    
    public class PatientManager extends JFrame {
    
        private STree <Patient> tree = new STree <Patient>();
    	Iterator <Patient> iter = tree.iterator();
    	FileReader reader = null;
    	FileWriter writer = null;
        Patient element = null;
    
    public void openFile() {
    
            try {
    
                reader = new FileReader("patients.txt");
    
            } catch (FileNotFoundException ex) {
                JOptionPane.showMessageDialog(null, "There was an error locating your file. Please try again", "ERROR LOCATING FILE", JOptionPane.ERROR_MESSAGE);
            } catch (NullPointerException npe) {
                JOptionPane.showMessageDialog(null, "There was an error locating your file. Please try again", "ERROR LOCATING FILE", JOptionPane.ERROR_MESSAGE);
            }
            readFile();
        }
    
        public void readFile() {
    
            for (Scanner in = new Scanner(reader); in.hasNextLine(); tree.add(element)) {
    
                String input = in.nextLine();
                String[] section = input.split(",");
                element = new Patient(section[0], section[2], section[4], section[3], section[1]);
                System.out.println(element.toString()); //check to see if elements are being constructed ** must delete
            }
            displayFile();
    
        }
    
        public void displayFile() {
    
    		int count = 0;
    
            textArea.setText("Patient ID" + "\t" + "Full Name" + "\t" + "   Age" + "\t" + "Visit Date" + "\t" + "Description" + "\n"
                    + "***********************************************************************************\n\n");
    
    
            while (iter.hasNext()) { //iterator is not entering loop **nothing is displayed to screen
    
    			Patient p = iter.next();
    			textArea.setText(textArea.getText() + "\n" + p.toString());
    			count++;
    		}
    
    		textArea.setText(textArea.getText() + "\n\n" + "Total " + count + " entries found");
    
    
        }

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    That's one of the most unusual for loops I've ever seen (generally that approach I'd see converted into a while or do/while instead of a for)! Looks to me like its alright though, but you may want to walk that tree through a debugger after the loop to be 100% certain that it added data properly.

    I'll ignore your gui completely here as well. In the displayFile's while loop, can you write some system.out.println's in there to confirm that it isn't actually iterating? If it is not iterating, ensure that the tree has data in it, and try extracting your iter inside the method (I'm not sure if Java likes having its iterator declared as a member, though logically I'd think that would work).
    If that still checks out: data in tree, iterator valid, but not iterating then post the code used for tree (or more specifically, the iterator for tree if its a different class). The fault will fall against the implementation of the Iterator. Also make sure to note what type of iteration you are using for the tree.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    rusty_fork (02-10-2011)

  • #3
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I figured out the error. I had to declare a new instance of the Iterator as a local data member instead of global. Everything is working fine now..

    Thanks for your help!

    Rusty

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by rusty_fork View Post
    I figured out the error. I had to declare a new instance of the Iterator as a local data member instead of global. Everything is working fine now..

    Thanks for your help!

    Rusty
    Yeah, I kinda thought that was going to be the case on this one (I've always extracted iterators on a need to use basis, and I can't recall if that's simply the way I've always done it or if I started doing that because I had iterator problems). I'm glad you got it sorted!
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

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