[Logo]
Forums Register Login
search in an arraylist
Hello, I am completely new with programming and Java.
I bought a book leurning java with exercises
I use for programming Blue J

I am working on a exercise about Array list.  I created two classes.
One class is groceries and one is Product

I would like search in the arraylist to look for the product and print in rerminal window with a seach commando. this is string

the words in code are in Dutch language :-)

     public void printBoodschappenMet (String zoeken)
     {
         boolean flag = zoeken.contains("");
        
         for(int part = 0; part < lijstje.size();part++)
         {
                    if (zoeken.contains("") & (flag = true))
                    {
                           
                    System.out.print(lijstje.get(part).getzoeken());
                }
               
                else
                    {
                            System.out.print("niets gevonden");
                        }
                    }  
Welcome to the Ranch Peter!

You forgot to say what the problem is that you are facing?
Welcome to the Ranch!

When posting the code, please use code tags. You can see how that would look like if you were use the code tags:
(1 like)
Hello,

thanks for the assistance!
where can i find the code tags for the next time?

My problem is that the seach assignment dont work when i looking for a specifically word in the array list.
Every line in from the array list is printed in the terminal window.
 

Peter van der Werff wrote:My problem is that the seach assignment dont work when i looking for a specifically word in the array list.


Please specify which line/-s in your code doing that?

Regarding code tags, within post edit panel there is a button Code, so you select your code and click that button.
I haven't run the code, but I'd recommend reviewing the syntax usage of the '&' character for bitwise / boolean operations, and the '=' character for assignment / equality operations to make sure you're using them correctly (spoiler: you're not using them correctly)
Welcome to the Ranch (again)

Try here for information about code tags (easy) or here (scroll to the bottom of the page) for more difficult topics about code tags.

And, I can see what Tim means about there being an error. I can see another potential error: what is going to happen if you try searching any text for ""? I am afraid the API documentation about that method doesn't give enough details to solve that error.
Hello,

I worked on the code but there are still problems.
I would like search in the arraylist to look for the product and print in rerminal window with a seach commando. this is string
this don't work. When i use the String "zoeken" in Englisch Search than goes the code always to  the "else" loop and print in the terminal window
" niets gevonden" / nothing found.

What do i wrong?

hi Peter,

by the look of it, 'lijstje' is a (small) List or array of Products. Now, in this list you want to find a Product which name = 'zoeken'. 'zoeken' is the parameter of the method.
So what must be done is something like:
You would only want to iterate the loop and test for equality if you have been told this is an exercise in writing loops. You would first have to ensure that whatever you are seeking has a correctly overridden equals() method (Strings do) and then use methods on the List like this or this. Note that in the case of Strings only, there is an alternative to the equals method: this.
Everyone,

thanks a lot for helping me!


I learned a lot
 

Peter van der Werff wrote:Everyone,

thanks a lot . . .

That's a pleasure

I learned a lot

That is always good to hear, too
Hello,

I am stil working on exercises about  Array list. also quite interesting
How can I do the easiest way a reverse searching a Arraylist?

I would like the last post on the index the fist searched with a seacht command.

My code don't  do that. The code goes the index from the fist entry in the list.


what am I doing wrong?


       
Suppose the list does indeed contain the String 'search'. The while-condition will then always be true. So there is a danger of having an infinite loop. However, 'part' will become negative at some point (in fact, right away since part starts at 0), and so list.get(part) will give an error. You should therefore start with 'part = list.size'.

But as I remarked in my previous post: there, list was a List of Products, not a List of Strings, so the epression 'list.contains(search)' will always be false.

So, three things to improve:
1) the starting value of 'part'
2) you should have another while-condition (one with 'part' involved)
3) you should test each Product from list if the name is suitable
@ Piet Souris

dear Piet Souris, thans for the post.

I do not understand yet the difference between List of Products and List of Strings.
The list of Prodoct are a string. I have imported these products with a string.
Do not keep them the variable string in a Arraylist, only the indexnumber in an Arraylist?
You're welcome!

Look at your opening post. You talk about two classes, Groceries and Product. A Product is therefore certainly not a String. Your list is a List of Products, and again, therefore not a List of Strings, even though you may be creating a Product by means of a String.

Can you show us your Product class?
Hello,  of course.

I would like to show the Product class?

hi Peter,

thanks!

Your 'Product' class has two members, the String 'naam' and the double 'hoeveelheid' ('quantity'). When you create an instance, you suplly the name and the quantity. Nothing wrong here.

Now, suppose you have a room full of people. Everyone has a name, like every instance of your Product. You want to know how many 'Peters' are in the room. Well, no one has written his name on his forehead, so you have no choice than to ask each person if his name is 'Peter'. How else would you find one?

Coming back to the list of Products, you have to ask each Product if its name is equal to 'search''. So, your method should be like:
Hi Piet Souris,

Thanks a lot for the great explanation!

I begin to understand how iit woks with a Arraylist.
To start with programming is difficult but very interesting :-)
hi Peter,

glad to be of help.

As you know: "alle begin is moeilijk". But that is how we all started, and the reward of holding on is what you rightly say: interesting and very rewarding! Enjoy the rest of the study, and don't hesitate to come up with more questions, if and when needed!
Did you explain that you are searching the List backwards? I suggest you enhance the explanation to show how that loop will stay within the limits of the size of the List. Also please explain why it works better with an array list than with a linked list.
OP: Have you found the contains() method of List, or these two methods: 1, 2?
Some beginners' exercises require the students write out the algorithm for such methods themselves.
 

Peter van der Werff wrote:Hello,

I am stil working on exercises about  Array list. also quite interesting
How can I do the easiest way a reverse searching a Arraylist?

I would like the last post on the index the fist searched with a seacht command.

My code don't  do that. The code goes the index from the fist entry in the list.


what am I doing wrong?

(...)


hi Campbell,

my replies were mostly based on this reply from Peter. He's working with an ArrayList, so I saw no reason to start talking about LinkedLists. I do not understand what 'list.contains' or 'list.lastIndexOf' would be good for, since the list is a list of Products and the search argument is a String.
As for staying within the bounds of the Array indices: OP himself used the construct 'part--' in one of his code snippets, and I kept it that way. I reckon he is capable enough of finding out why this works!

And if I left out some explanations that should have been there, then I apologize. I hope that OP will come with some additional questions in that case.
Points taken

This thread has been viewed 924 times.

All times above are in ranch (not your local) time.
The current ranch time is
Nov 22, 2018 04:23:11.