Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By Tolls

Thread: two line errors

  1. #1
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default two line errors

    Here is the code that was given to me. I have not done anything but typed it into an IDE from a piece of paper that was given to me. There are two lines that have errors.

    ***NOTE*** you MAY NOT need to read through the code just copy and paste the code in an IDE (I'm using NetBeans) and look at the two lines in the BSTpractice class that have an error and let me know if you have the solution to fix the errors.***End NOTE***

    Class node:

    Java Code:
    package bstpractice;
    
    public class Node {
        public String name;
        public int lowIndx, highIndx, //data[lowIndx..highIndx] = data items 
                                       //represented by subtree at nod;
                                       //data at node is data[mid], mid = 
                                       //(lowIndex+highIndex + 1) / 2;
                leftChildIndx, rightChildIndx; //>= 0; -1 means absense of child
        
        public Node (int lowIndx, int highIndx, int leftChildIndx, 
                     int rightChildIndx) {
            this.lowIndx = lowIndx; this.highIndx = highIndx; 
            this.leftChildIndx = leftChildIndx; this.rightChildIndx = 
                     rightChildIndx;
        }
        
        public String toString() {
            return ("lowIndx = " + lowIndx + "highIndex = " + highIndx + 
                     ((leftChildIndx >= 0) ? (" leftChildIndx = " + leftChildIndx)
                     : "") + ((rightChildIndx >= 0) ? (" rightChildIndx = " +
                     rightChildIndx) : ""));
        }
    }//class Node
    Java Code:
    Class BSTpractice:
    
    
    package bstpractice;
    
    import java.io.*;
    import java.util.*;
        
        public class BSTpractice {
            int root;
            int[] data = {-3, 2, 11, 12, 18, 23, 31, 32, 35, 42, 44};//sorted array
                                                                //of distinct items;
            int[] nodes; //data corresponding to nodes[i] is data[i]; this is used 
            //in searching for an x 
            
            public BSTpractice (int numNodes) { //numNodes <= data.length
                nodes = new Node[numNodes];  /*this is where I get an error*/
                root = BuildBST (0, numNodes - 1);
            }
            
            //recursive; returns root node num, i. e, index of associated data
            private int BuildBST (int low, int high){
               int root = (low + high + 1) / 2;
               //...fill in
               System.out.println("nodes[" + root + "]: " + nodes[root].toString());  //this is where i get an error
               return (root);
            }
     
            //non-recursive; returns search-path
            public ArrayList<Integer> SearchPath (int startIndx, int endIndx, int x)
            {
                ArrayList<Integer> searchPathDataIndices = new ArrayList<Integer>();
                int origStartIndx = startIndx, origEndIndx = endIndx;
                while (startIndx <= endIndx) {
                    //... fill in
                }
                return searchPathDataIndices;
            }
        
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            int numNodes = 9;
            BSTpractice bst = new BSTpractice(numNodes);
            System.out.println();
            System.out.print("data []: " + Arrays.toString(bst.data));
            System.out.println(" (only the first " + numNodes + "  items used)");
            System.out.print("Hit return-key to continue successive runs of "
                            + "SearchPath");
            scan.nextLine();
            bst.SearchPath(0, numNodes - 1, 12);
            scan.nextLine();
            bst.SearchPath(0, numNodes - 1, 20);
            scan.nextLine();
            bst.SearchPath(0, numNodes - 1, 10);
            scan.nextLine();
            bst.SearchPath(0, numNodes - 1, -5);
            scan.nextLine();
            bst.SearchPath(0, numNodes - 1, 43);
            scan.nextLine();
            bst.SearchPath(0, numNodes - 1, 32);
        }
    }
    Please don't respond with whats this suppose to do. I don't know I just want to code to run since it has to run before I can start working on it. Please only respond with an answer to the original question. Please.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: two line errors

    Quote Originally Posted by jwl View Post
    ... Please only respond with an answer to the original question. Please.
    Am I reading this correctly, that you're asking for free help from volunteers, and you're placing stipulations on what comments and answers are allowed? Including requests for clarification? This may not be the best tactic.

    Is it OK to ask you to post your error message? Or is this forbidden too?

    Edit: the key is likely to be found in the error message, so you will want to post this in the future for all similar questions. Note the type of variable that you declare nodes to be -- an int array. Note the type that you assign it to, a Node array. Perhaps the compiler is complaining about this? Again, you will want to post the error message as this information will be contained therein.

    And again, you will likely not want to put stipulations on the types of things folks post here as that's not quite fair. Some may say that if you want to place conditions on the help you receive, then perhaps you should pay for it. Others may say, that since this is a volunteer forum, you should be thankful for anyone who takes time to consider your question and reply to it, even if what they post doesn't answer the direct question. Sometimes we don't get to the answer directly, sometimes the path we take can meander, as long as there's progression, then that's a good thing.

    As always, if someone posts something rude or inappropriate, then of course, let us, the moderators know, and we can delete it or edit it.
    Last edited by Fubarable; 11-21-2012 at 03:32 AM.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: two line errors

    Yeah, posting the full and exact error messages would help a lot. Despite your lack of relevant info and unrealistic limitations I am feeling benevolent today.
    Java Code:
    nodes = new Node[numNodes];  /*this is where I get an error*/
    The nodes variable was declared as in int array not a Node array. This akin to declaring a boolean variable and trying to cram a String into it.

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default Re: two line errors

    Quote Originally Posted by jwl View Post
    Please don't respond with whats this suppose to do. I don't know I just want to code to run since it has to run before I can start working on it.
    Since others have picked the obvious points, I'll take this one. Sometimes (indeed, often) the solution to an error requires knowledge of what the code is supposed ot be doing. Just getting it to compile, or just run without exceptions, is rarely enough.

    Besides, if you don't know what it does then how do you expect to be able to work on it?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default Re: two line errors

    Quote Originally Posted by Fubarable View Post
    Am I reading this correctly, that you're asking for free help from volunteers, and you're placing stipulations on what comments and answers are allowed? Including requests for clarification? This may not be the best tactic.

    Is it OK to ask you to post your error message? Or is this forbidden too?

    Edit: the key is likely to be found in the error message, so you will want to post this in the future for all similar questions. Note the type of variable that you declare nodes to be -- an int array. Note the type that you assign it to, a Node array. Perhaps the compiler is complaining about this? Again, you will want to post the error message as this information will be contained therein.

    And again, you will likely not want to put stipulations on the types of things folks post here as that's not quite fair. Some may say that if you want to place conditions on the help you receive, then perhaps you should pay for it. Others may say, that since this is a volunteer forum, you should be thankful for anyone who takes time to consider your question and reply to it, even if what they post doesn't answer the direct question. Sometimes we don't get to the answer directly, sometimes the path we take can meander, as long as there's progression, then that's a good thing.

    As always, if someone posts something rude or inappropriate, then of course, let us, the moderators know, and we can delete it or edit it.

    Yes, I have time limits and extra work and thought maybe this would help me get an answer quicker than previous posts. Because my last four post were responded to days after my programs are due. Obviously this is not a really good place for my needs. at least while I'm in school. It's nothing personal. Sorry.

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default Re: two line errors

    Forums won't be good then, because few will react the way you seem to want them to.
    Few will copy and paste code into an IDE to see what's wrong.

    Those who get the quicker answers are those that provide all the relevant details, easily identified in the post itself.
    So errors are supplied in full, along with a note as to the line they are occurring on.
    Same with exceptions.
    If there's a problem with the output, then the output is supplied along with what the output was expected to be.
    Maybe signs that some debugging has been done.

    If you find that there are lots of questions being asked when you post threads then it is a fair indicator you are not providing enough, relevant, information.

    Note the "relevant"...overdoing it is just as bad.
    Fubarable likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: two line errors

    Quote Originally Posted by jwl View Post
    Yes, I have time limits and extra work and thought maybe this would help me get an answer quicker than previous posts. Because my last four post were responded to days after my programs are due. Obviously this is not a really good place for my needs. at least while I'm in school. It's nothing personal. Sorry.
    Then best of luck, but if you go to other forums, you'll find that same as here, the better and more complete questions get quicker and better replies, and that they too will bristle at unwarranted stipulations. As I see it your options include hiring a tutor or if you come back here or to other forums, writing better questions since this is a learned skill that can improve with practice and effort. You can find help on the latter here: Jon Skeet -- Writing the perfect question

Similar Threads

  1. Replies: 16
    Last Post: 09-17-2011, 02:41 AM
  2. Replies: 4
    Last Post: 01-04-2011, 12:36 AM
  3. Replies: 3
    Last Post: 03-08-2009, 01:51 AM
  4. Replies: 22
    Last Post: 01-05-2009, 01:03 AM
  5. Replies: 7
    Last Post: 12-08-2008, 06:20 PM

Posting Permissions

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