• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Objects and methods

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, a search method usually returns the index where you found the match, rather than true/false.
 
John Stevenson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Luke wrote: . . . Winston suggested writing it it out . . .

Winston???
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don’t let Winston see that; he’d be shocked
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
John Stevenson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One thing you can improve: the method name doesn't match very well with what the code does. Here are some suggestions:
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your add() method is not correct. That loop will not execute even one time.
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic