File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Objects and methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Objects and methods" Watch "Objects and methods" New topic
Author

Objects and methods

John Stevenson
Greenhorn

Joined: Feb 09, 2013
Posts: 5
Hello I have a few questions about a program i am writing. The methods I am writing are not working properly when executed in the main. The first method i wannted to address that is not working properly is search.. Here is the code thus far. It is


The search method when called in main is supposed to decide if the user id has been used already. After running the main and going through again entering the same user id does not throw out an error and it acts as if it has never been used even though it is a duplicate. There are more classes in this program and i can send them if you need me to. Thanks for taking the time to look at this.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

I'm not sure what your search(String) method is supposed to do -- I expect the name "search" tries to explain it, but comments would be helpful. However one problem I can see right away is that the code in that method doesn't ever use the String parameter passed to the method. So that would be a problem.
John Stevenson
Greenhorn

Joined: Feb 09, 2013
Posts: 5
Good point. Here is my revised (and non-working attempt at a meaningful search method):

I added a getId method but it is unable to see the method in the same class and says the
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Welcome to the Ranch
When you say non‑working attempt, it sounds as if you were guessing. That is a labour‑intensive way to get a correct program: you guess differently 1000000 times and there is a good chance that one of those programs will be correct. All you then have to do is work out which one
The alternative takes several steps. You need four pieces of hardware. One is the lead which connects your computer to the power supply. Lay that on the table so you can see both ends. The others are pencil paper and eraser. You write down, very carefully, in English (or other natural language) exactly how you intend to do the search. You refine the search process until you have words of one syllable only. Then you can reconnect your computer and try turning it into code.

Then you will have 999999 fewer versions than before, of which 1 will work
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
By the way, a search method usually returns the index where you found the match, rather than true/false.
John Stevenson
Greenhorn

Joined: Feb 09, 2013
Posts: 5
Actually what i meant by non working attempt is just that, my attempt at a method body that ended in no results. I understand what your saying. My problem is that im confused at a deeper level of my program and do not really understand the core idea of how to write the classes properly. This is going to lead to problems for me. Perhaps you can help interperet a few things for me and then we can go from there?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

In fact your problem isn't about classes or methods. It is the algorithm in the Method which is incorrect. That is why Winston suggested writing it it out until you yet the method to do what you want.


Steve
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Steve Luke wrote: . . . Winston suggested writing it it out . . .
Winston???
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Campbell Ritchie wrote:
Steve Luke wrote: . . . Winston suggested writing it it out . . .
Winston???


Doh! Sorry. Don't know why I thought it was Winston in this thread...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Don’t let Winston see that; he’d be shocked
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
John Stevenson wrote: . . . Perhaps you can help interperet a few things for me and then we can go from there?
One thing at a time. You can’t expect to write a whole class at your stage. Get one method working and then you can try the others.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5288
    
  10

Your search will only work if the thing you're looking for is the last element of the array. Otherwise, the result you get will always be 'false' which may or may not be correct.

To find your bug, ask yourself this: Will the loop end immediately when a matching key is found or does it continue iterating through the rest of the array? If it keeps iterating through the array after finding a matching key, does 'found' ever get assigned a new value?


Junilu - [How to Ask Questions] [How to Answer Questions]
John Stevenson
Greenhorn

Joined: Feb 09, 2013
Posts: 5
So this is where i got to so far i pretty much changed it all around

but im getting errors in another part of the program hahaha so i can't test it at this time?
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5288
    
  10

One thing you can improve: the method name doesn't match very well with what the code does. Here are some suggestions:
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5288
    
  10

Your add() method is not correct. That loop will not execute even one time.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
You would appear to be using a binary search. Why? You realise that only works on a sorted array?

It still looks as though you were guessing, and I think that search method will not work.
John Stevenson
Greenhorn

Joined: Feb 09, 2013
Posts: 5
Oh yeah i forgot about the sorted array requirement for the binary search. I have an array of Doubly linked lists, and what i need to do is add items to each of the linked lists and the 3 linked lists in turn make up an array. The array will have many items(which each item is 3 doubly linked lists and the main structure is going to hold all of the array items. I have a find method in my linked list class and there is a contains method in my array class which calls the find method from linked list. Each linked list is an IndexRecord(which has it's own class) I have an add method that collects the 3 linked list elements as strings and incorporates them into the array.
This is the IndexRecord class:

This is the main structure record class:

This is my find method in the DoublyLinkedList class:

This is where it is the array class:




I am confused about the way this structure is ultimately supposed to be built. Am i supposed to have a whole bunch of seperate arrays that make up one DataRecord? Or should i have a bunch of DataStructure array elements? My add method is wrong and where i am getting confused is how to implement the info brought in through the add method and distribute it out into the dataStructure array? Thanks for your time.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Objects and methods