aspose file tools
The moose likes Java in General and the fly likes Converting String to ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Converting String to ArrayList" Watch "Converting String to ArrayList" New topic
Author

Converting String to ArrayList

john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495

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


“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” (Mosher's Law of Software Engineering)
“If debugging is the process of removing bugs, then programming must be the process of putting them in.” (Edsger Dijkstra)
Sai Hegde
security forum advocate
Ranch Hand

Joined: Oct 26, 2010
Posts: 183

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.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16483
    
    2

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
Ranch Hand

Joined: Oct 26, 2010
Posts: 183

Nice catch, Paul.
john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495


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
Bartender

Joined: Oct 14, 2005
Posts: 16483
    
    2

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
Ranch Hand

Joined: Oct 26, 2010
Posts: 183

Why not do it the reverse way

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 1788
    
    2
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

Joined: Feb 24, 2011
Posts: 495

SO something like this?
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 1788
    
    2
No need for a loop. The contains method searches the Set:

Sai Hegde
security forum advocate
Ranch Hand

Joined: Oct 26, 2010
Posts: 183

Yes, like I mentioned before. Do a contains on your collection rather than the other way around.
john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495

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

Joined: Feb 24, 2011
Posts: 495

This works though:
john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495

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
Ranch Hand

Joined: Oct 26, 2010
Posts: 183

john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495

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
Ranch Hand

Joined: Oct 26, 2010
Posts: 183

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

Joined: Feb 24, 2011
Posts: 495

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
Ranch Hand

Joined: Jun 13, 2009
Posts: 1788
    
    2
It was wrong before and its still wrong now.
john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495

Rob, are you talking to me?
If so, I do not understand how I am.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 1788
    
    2
Well, I'm tired of repeating myslelf.
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

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. !@*%#
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Converting String to ArrayList
 
Similar Threads
problems printing from array list in hashmap
SpellChecker Again... What am I doing wrong?
String[] vs ArrayList, very simple
String to char to string to int: more efficient way?
Cannot find bean: "EMPLOYEELIST" in any scope