Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Converting String to ArrayList

 
Ranch Hand
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am writing a Client and Server instant message application. It is well on its way with multiple color support, private messaging, hacker protection, mod privileges (only me for now) that can boot people off permanently or once, etc, etc. Anyway, my point is that it is working fine and well. In my clients, I have a filter. It filters out bad words for both the name and the message. It looks something like this:

There is about 500 lines of code just doing this. I thought it would be more efficient speed wise, simplicity wise, and OOP wise to do this in an ArrayList.
I have never actually used one so I just wanted to be sure I can do this...
Here is what I think I can change this to...

I was wondering whether this would work and if I am doing it right. Thanks guys!
cc11rocks aka John Price
 
security forum advocate
Posts: 229
1
Android Flex Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, Don't you think it is a mantainability issue having all the string constants defined in your code?
Pull them out to a properties file and then check your input against it.
You could also have enums defined for both just in case and check against those.
 
Sheriff
Posts: 26947
83
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 don't even think it will compile. (Didn't you try it?)

Anyway you don't want to see if a String contains an ArrayList of Strings, because that doesn't make any sense. You want to see whether the String contains any of the Strings in the ArrayList. For which you have to loop through the ArrayList and consider each individual String.
 
Sai Hegde
security forum advocate
Posts: 229
1
Android Flex Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice catch, Paul.
 
john price
Ranch Hand
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The first example I showed does in fact work. The second example I did not try. I have not used ArrayLists before and was kind of confused how to get this working. Yes, I have read the APIs. Would this work?
Thanks,
John Price aka cc11rocks
 
Paul Clapham
Sheriff
Posts: 26947
83
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
Would it work? You should know by now that the answer to this question, in the programming context, can never be "yes". At best it can be "probably", because there's always some little thing you didn't mention. But yeah, probably it would work. I would use a so-called "new" for loop (been in the language for about 8 years now) and I would break out of the loop as soon as I found a bad word:

No point in continuing to check the other 859 bad words when you've found one already!
 
Sai Hegde
security forum advocate
Posts: 229
1
Android Flex Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not do it the reverse way

 
Rancher
Posts: 3253
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should probably be using a Set not a List for this. A Set is more efficient when doing the search for a given Object.
 
john price
Ranch Hand
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SO something like this?
 
Rob Camick
Rancher
Posts: 3253
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No need for a loop. The contains method searches the Set:

 
Sai Hegde
security forum advocate
Posts: 229
1
Android Flex Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, like I mentioned before. Do a contains on your collection rather than the other way around.
 
john price
Ranch Hand
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried it and it only works if the JTextField is EXACTLY the badword. Example code:


if I change line eight to

or

The output is:

or

How do I fix this?
If I try to do the string.contains(stringarray), which would work, it return an error that that cannot happen.
Thanks,
John Price aka cc11rocks
 
john price
Ranch Hand
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This works though:
 
john price
Ranch Hand
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Found the solution to second to last post (from this post):
it should be
badWord not badWords as badWord is a String and badWords is an ArrayList.
Which of the above solutions should I use?
Thanks,
cc11rocks aka John Price
 
Sai Hegde
security forum advocate
Posts: 229
1
Android Flex Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
john price
Ranch Hand
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sai Hedge, your code does not work. It doesn't return any errors, but it doesn't work. Here is your compilable code (had to modify 2 things):


,
cc11rocks aka John Price
 
Sai Hegde
security forum advocate
Posts: 229
1
Android Flex Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure, it wouldn't like the way you are running it.
How do you even get the Swing Frame to show up. The JTextField that you are using is a Swing component and should be used within a Panel/Form(some Swing) container.

For what you are looking at, here is the code. Now you'd need to dig into more on the Swing parts.

 
john price
Ranch Hand
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is an example. My real code is over 600 and 800 lines (two versions of the program, one for mods and users). Currently, I am using this way:
 
Rob Camick
Rancher
Posts: 3253
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It was wrong before and its still wrong now.
 
john price
Ranch Hand
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob, are you talking to me?
If so, I do not understand how I am.
 
Rob Camick
Rancher
Posts: 3253
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I'm tired of repeating myslelf.
 
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to split the text input up into words so that you can check each one against your Set.



Outputs
This is. A !@*%#, test-sentence. !@*%#
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic