Thread: Method help

Page 2 of 2 First 12
  • Jump to page:
    #16
  1. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    the add method still having the other problem
    Please explain what the other problem is.
  2. #17
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    16
    Rep Power
    0
    1 Add a new student
    0 End
    1
    Enter a unique ID number to add
    1234 //New id number here
    Enter first name
    Ernest
    Enter last name
    Bollard
    1 Add a new student
    0 End 1
    Enter a unique ID number to add
    1234 //Same id number again here
    Enter first name // Allowed me to continue anyway
    Kim
    Enter last name
    kommy
    My search method is not working properly here and i was wondering where i am going wrong with it?
  4. #18
  5. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Try debugging the code by adding some println() statements to print out the values of all the variables that the code looks at to see where the logic is going wrong.

    The Arrays class's toString() is a useful method for formatting arrays for printing:
    Code:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
  6. #19
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    16
    Rep Power
    0
    Here is the method i currently have
    Code:
    public boolean search(String key) 
        {
    	int j;
    	for(j =0; j<nElems; j++)
    	{
              if(a[j].key.equals(key))
              {
                 found = true;
               }
             else
            {
              found = false;
            }
        }
        return found;
       }
    Here is how i implement it:
    Code:
     do
            {
                System.out.println("Enter a unique ID number to add");
                tempID=keyboard.nextLine();
                found = s.search(tempID);
                if (found)
                {
                    System.out.println("ID already in use");
                    System.out.println("Please re-enter a unique ID");
                }
            }
           while (found);
    That is where i am at. The method does not produce a found when the number has already been entered and that is where i have come to the conclusion that my method or it's implementation is wrong but i don't really understand how?
  8. #20
  9. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Debug the search() method by adding println statements to it as I described last post.
    You need to see what values the computer is looking at to see why it is working the way it does.
  10. #21
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    16
    Rep Power
    0
    The print statements are not showing up in the console
  12. #22
  13. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Are you executing the code with the print statements or an old version without them.
    Where are the print statements located? Is there any way execution can skip over them?
  14. #23
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    16
    Rep Power
    0
    Yes i moved the print statements around and shuffled everything up and still nothing.
  16. #24
  17. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    If nothing prints then the println statements are not being executed. Add more println statements to show where the execution is progressing from the start. The last println that prints will show you where the execution has ended.
  18. #25
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    16
    Rep Power
    0
    This approach doesn't seem to be working for me
  20. #26
  21. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    What is printing on the console? If nothing is printing, then you are not executing the code with the println statements. Make sure the code with the println statements compiles without errors and that you are executing the new stuff.
  22. #27
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    74
    Rep Power
    19
    Originally Posted by fronter
    Here is the method i currently have
    Code:
    public boolean search(String key) 
        {
    	int j;
    	for(j =0; j<nElems; j++)
    	{
              if(a[j].key.equals(key))
              {
                 found = true;
               }
             else
            {
              found = false;
            }
        }
        return found;
       }
    You are not initializing the found variable.
    What datastructure is the a[] array? In your previous example you initialized this to null, and then you started searching.
    Also, in this code above, you should return true as soon as you found the key, because this code will always return false, if the key is not at the last element in this list.
  24. #28
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    16
    Rep Power
    0
    Oh sorry I didn't send the full class. The methods and variables are in a class called DataStructure. DataStructure consists of 3 IndexArray objects: fName, lName, tempId. DataRecord is a class that consists of the 3 records and has the constructor and accessor methods. Each of these three IndexArray are to be dynamically sorted as the data comes in (add()) Boolean found is initialized static in the DataStructure class but you didn't see it in what I've posted thus far. a[] is type IndexArray I have to search and apply methods to these 3 sub arrays (IndexArrays) which are apart of a large array (DataStructure).
Page 2 of 2 First 12
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo