• 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

finding string in list with partial contents

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a List (or array) of strings. {"some string 1", "some string 2","fred smokes","tim drives", "bob flys", "mary drives"};

I want to know if any of the individual strings contains 'drives', and if so how many/which ones.

I 'think' I have to make my own subclass.
 
Ranch Hand
Posts: 75
Android Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check the String class, there's a method in there that tells you if a string "contains" another string you are looking for.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sam detweiler wrote:I 'think' I have to make my own subclass.


This is not where you should start thinking.

you start by thinking in English (or any other natural language you like). Write out the steps you would take if you had pencil and paper only.

1) Look at each string one at a time
2) see if it contains the word "fred"
3) If so, print it

Then you revise the steps, pretending you are telling a 10 year old child how to do it. refine each step, making them simpler and more granular.

Only after you have done that should you think about writing any java code.
 
sam detweiler
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aj Prieto wrote:Check the String class, there's a method in there that tells you if a string "contains" another string you are looking for.



yes, but I have an arraylist of strings.

I can use arraylist.indexOf('string') to find if the ENTIRE string is in the list.. -- this fails when only a part of the string is searched for

what I want is is the elements of the list where a PART of the string exists.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sam detweiler wrote:

Aj Prieto wrote:Check the String class, there's a method in there that tells you if a string "contains" another string you are looking for.



yes, but I have an arraylist of strings.

I can use arraylist.indexOf('string') to find if the ENTIRE string is in the list.. -- this fails when only a part of the string is searched for

what I want is is the elements of the list where a PART of the string exists.




How about using a loop along with the previous suggestion?

Henry
 
sam detweiler
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:

sam detweiler wrote:I 'think' I have to make my own subclass.


This is not where you should start thinking.

you start by thinking in English (or any other natural language you like). Write out the steps you would take if you had pencil and paper only.

1) Look at each string one at a time
2) see if it contains the word "fred"
3) If so, print it

Then you revise the steps, pretending you are telling a 10 year old child how to do it. refine each step, making them simpler and more granular.

Only after you have done that should you think about writing any java code.



thank you.. but I did not expose my thinking process. which may have included may more steps than you have outlined.

my conclusion remains, unless someone says.. use method foo().. that I will need to make some additional coding of some sort or another.
 
sam detweiler
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

sam detweiler wrote:

Aj Prieto wrote:Check the String class, there's a method in there that tells you if a string "contains" another string you are looking for.



yes, but I have an arraylist of strings.

I can use arraylist.indexOf('string') to find if the ENTIRE string is in the list.. -- this fails when only a part of the string is searched for

what I want is is the elements of the list where a PART of the string exists.




How about using a loop along with the previous suggestion?

Henry



yes.. I ended up writing my own routines.
 
Aj Prieto
Ranch Hand
Posts: 75
Android Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sam detweiler wrote:
my conclusion remains, unless someone says.. use method foo().. that I will need to make some additional coding of some sort or another.



In my post, I pretty much said to use method foo() (read it again carefully).
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did not mean for that to be the end of the process...that was the fist step in coming up with "many more steps".

The idea is you think about WHAT needs to be done first. Once you know that, then you start thinking about HOW to do it.

By thinking through what needs to be done, you may realize that one of the things you need to do is look at each and every string in your array or list by itself. So then you think about how you pull them out one at a time - the loop Henry mentions.

Then you have a String...now you need to look at how you check to see if a given String has a specific sub-string. You should realize that it doesn't matter how you got the initial String, so you can focus on just the searching part.

you work through it a piece at a time, and only code a piece at a time. You don't try and do it all in one go.
 
We're being followed by intergalactic spies! Quick! Take this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic