Results 1 to 5 of 5
  1. #1
    mxcnrawker is offline Member
    Join Date
    Apr 2012
    Location
    San Jose Ca
    Posts
    16
    Rep Power
    0

    Default Outputs for user input and instantiated parameter don't match, any suggestions?

    The following program is designed to take a String array of objects and to sort them into a binary search tree. After the tree has been made, then the user
    can enter a name that will give them ancestors of that member in the tree.

    Java Code:
    // This is the FamilyTree class created to create the tree
    
    class FamilyTree
    {
    	// List of fields in the FamilyTree class
    	private String[] stringArray;
    	
    	// Creating a constructor that will initialize the class.
    	// NOTE: We will sort out the paramater in acceding order
    	public FamilyTree(String[] names)
    	{
    		// List of local variables in the FamilyTree class
    		String unsortedValue;
    		int scan;
    		
    		// Initiliazes the length of the array.
    		stringArray = new String[names.length];
    		
    		// Creating a loop that will copy each element in the array.
    		for (int index = 0; index < stringArray.length; index++) 
    		{
    			stringArray[index] = names[index];
    		}
    		
    		// Creating a loop that will iterate thorugh the entire list.
    		for (int index = 1; index < names.length; index++) 
    		{
    			// Redirects the unsortedValue to the second element in the list.
    			// NOTE: Assumes the first element is already sorted.
    			unsortedValue = names[index];
    			
    			// Redirects the scan variable to the second position.
    			scan = index;
    			
    			// Creating a loop that will compare elements and go through the sub-list.
    			while (scan > 0  && names[scan - 1].compareTo(unsortedValue) > 0) 
    			{
    				names[scan] = names[scan - 1];
    				scan--;
    			}
    			
    			// Inserts the value to its proper position.
    			names[scan] = unsortedValue;
    		}
    	}
    	
    	// Creating a private Node class that will store the elements in the binary tree.
    	private class Node
    	{
    		// List of fields in the private Node class.
    		String name;
    		Node left;
    		Node right;
    		int height;
    		
    		// Creating the root of the binary tree.
    		Node(String value)
    		{
    			// References the first constructor
    			this(value, null, null);
    		}
    		
    		// Creating a constructor that will create the nodes on the list.
    		Node(String n, Node l, Node r)
    		{
    			name = n;
    			left = l;
    			right = r;
    		}
    	}
    	
    	// Creating the root of the new tree.
    	private Node root = null;
    	
    	// Creating a method that will sort the list into a binary search tree.
    	public void arrayToBST(String[] stringArray)
    	{
    		// List of local variables in addToTree method.
    		int start = 0;
    		int end = stringArray.length - 1;
    		
    		root = addToTree(stringArray, start, end);
    	}
    	
    	
    	// Creating a method that will add the elements in the tree.
    	// NOTE: the array has already been sorted, so the last element is the highest
    	public Node addToTree(String[] stringArray, int start, int end)
    	{
    		// List of local variables in the addToTree method.
    		int middle;
    		
    		// Returns null if there are no elements in the list
    		if (start > end) 
    		{
    			return null;
    		}
    			else
    			{
    				// Calculates the middle value.
    				// Avoids overflow of array
    				middle = (start + end) / 2;
    				
    				// Lets the middle value be the root.
    			 	Node root = new Node(stringArray[middle]);
    				
    				// Adds elements to the left of the tree
    				root.left = addToTree(stringArray, start, (middle - 1));
    				
    				// Adds elements to the right of the tree
    				root.right = addToTree(stringArray, (middle + 1), end);
    				
    				// Returns the new created tree
    				return root;
    			}
    	}
    	
    	// Creating a method that will find the node with the correspondning element.
    	public Node search(String name)
    	{
    		// Creating a reference to the root of the tree.
    		Node head = root;
    		
    		// Creating a loop that will search through the list for the node value.
    		while (head != null) 
    		{
    			if (name.compareTo(head.name) < 0) 
    			{
    				// Checks the left of the tree.
    				head = head.left;
    			}
    				else 
    				{
    					// Checks the right of the tree.
    					head = head.right;	
    				}
    				
    			if (head == null) 
    			{
    				// Returns null once it reaches end of tree
    				return null;
    			}
    		}
    		
    		// Returns the element that has been found.
    		return head;
    	}
    	
    	// Calls the public method that will display the elements in the tree.
    	public void ancestor(String name)
    	{
    		// List of local variables in the ancestor method.
    		Node found = search(name);
    		Node head = root;
    		
    		// Creates a loop that will go through the entire tree.
    		while (head.name != name) 
    		{
    			if (name.compareTo(head.name) < 0) 
    			{
    				// Checks the left part of the tree
    				System.out.print(head.name + " ");
    				head = head.left;
    			}
    				else 
    				{
    					// Checks the right part of the tree
    					System.out.print(head.name + " ");
    					head = head.right;	
    				}
    				
    			if (head == null) 
    			{
    				return;
    			}		
    		}
    		
    		return;
    	}
    }
    // End of the FamilyTree class
    
    // This is my driver test program to check if they match
    
    import java.util.Scanner;
    
    class Tester
    {
    	public static void main(String[] args)
    	{
    		String[] test = {"Carol", "Erica", "Alex","Karla","Leonard", "Bobby","Maggie", "Oscar", "Frank", "Jason", "Iris", "Hector", "Greg","Nancy", "David"};
    		String input;
    		
    		Scanner keyboard = new Scanner(System.in);
    		
    		// Prints out original list.
    		System.out.print("Original List: ");
    		for (String elements : test) 
    		{
    			System.out.print(elements + " ");
    			
    		}
    		
    		// Instantiates the FamilyTree object
    		FamilyTree fTree = new FamilyTree(test);
    		
    		// Creates space
    		System.out.println();
    		System.out.println();
    		
    		// Prints out sorted list from the constructor.
    		System.out.print("Sorted list: " );
    		for (String elements: test) 
    		{
    			System.out.print(elements + " ");
    		}
    		
    		// Adds array and arranges to binary search tree
    		fTree.arrayToBST(test);
    		
    		System.out.println();
    		System.out.println();
    		
    		// Output for user input
    		System.out.print("Enter a name: ");
    		input = keyboard.nextLine();
    		System.out.println();
    		System.out.print("User input: ");
    		fTree.ancestor(input);
    		
    		System.out.println();
    		System.out.println();
    		
    		// Output for same String as user input
    		System.out.print("String parameter: ");
    		fTree.ancestor("Carol");
    	}
    }
    The following is the output from my program after using Carol as my input.

    Outputs for user input and instantiated parameter don't match, any suggestions?-screen-shot-2012-12-12-1.10.12-am.png

    It would be greatly appreciated if you guys could tell me whats wrong, been working on this for the past 4 hours and I would love to get to the bottom of this
    before I move on to adding more methods to this class. Thanks a bunch!

  2. #2
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Outputs for user input and instantiated parameter don't match, any suggestions?

    I guess you missed the problem description... if you want answers you should tell us what goes wrong/is unexpected or do you expect us to guess?
    For me it works fine, it puts our names for your input, etc. it sorts the list, so what is wrong?
    Last edited by Sierra; 12-12-2012 at 11:19 AM.
    I like likes!

  3. #3
    mxcnrawker is offline Member
    Join Date
    Apr 2012
    Location
    San Jose Ca
    Posts
    16
    Rep Power
    0

    Default Re: Outputs for user input and instantiated parameter don't match, any suggestions?

    The problem is that, when I enter the a value, it gives me a totally different output, as in the picture, but then when value is already entered, is give me the correct answer that I am looking for. And Sierra you just pointed me in the right direction, so when you ran this code and entered the same value, you got the same output for both?

  4. #4
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Outputs for user input and instantiated parameter don't match, any suggestions?

    I just know that you usually never compare strings linke that: head.name != name ... rather than using the String.equals(...) method.
    Go through the program step by step and print the relevant parameters or use your IDE debugger.

    EDIT: Ok I guess you really want to check if that is the same object - so that is probably correct... still confusing though

    Check if your user input is 100% equal to the string ""Carol" you insert afterwards - as you compare string objects directly to each other (not only the content) this can make a huge difference - you should not do that.
    Last edited by Sierra; 12-12-2012 at 11:26 AM.
    I like likes!

  5. #5
    mxcnrawker is offline Member
    Join Date
    Apr 2012
    Location
    San Jose Ca
    Posts
    16
    Rep Power
    0

    Default Re: Outputs for user input and instantiated parameter don't match, any suggestions?

    rookie mistake.... thank you very much!

Similar Threads

  1. perfect method parameter/argument match rejected. why?
    By collegestudent115 in forum Advanced Java
    Replies: 5
    Last Post: 10-31-2010, 06:55 PM
  2. how to get input from User
    By Alvaro in forum New To Java
    Replies: 7
    Last Post: 01-15-2010, 11:02 PM
  3. Web service client and array as input parameter
    By mpouly in forum Other IDEs
    Replies: 4
    Last Post: 11-29-2009, 05:33 PM
  4. User input- Pop Up Box
    By dedachi in forum AWT / Swing
    Replies: 3
    Last Post: 03-23-2009, 04:47 AM
  5. Input parameter of Main method
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 07-12-2008, 06:24 PM

Tags for this Thread

Posting Permissions

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