PDA

View Full Version : Cs160 HW



darkfire23
10-20-2010, 03:51 AM
Hey guys, i got a question on my HW3 for CS160. It's a pretty basic problem using JAVA which reads in a file from input that is a paragraph with a few sentences. The object of the program is to bleep out all the "bad" words like f***, leaving the first letter. I'm getting my file to read in fine, but not sure how to start? I've built a for loop, but not really sure how it should reach each word then move to the next one. Any help would be greatly appreciated!
Thanks,
DF

Smartzkid
10-20-2010, 04:12 AM
Assuming the input isn't expected to be super long, you can read the entire file into a single string and then use String.indexOf(String str) to bleep one word.

http://download.oracle.com/javase/1.4.2/docs/api/java/lang/String.html

Example program flow:

Create array of 'bad' words
Read file into string
for each 'bad' word:
---find all instances of current 'bad' word, bleep
save file


This is going to take 2 loops, one to loop through the 'bad' word array (hint: for loop), and one to loop through the input string looking for multiples of the current 'bad' word (hint: while loop).

Sorry, I'm a bit scatterbrained right now, but I hope that helps.

darkfire23
10-20-2010, 04:24 AM
Don't worry I am too.. it's getting late, but that did help a little. I figured out how to get the loops set up, but here one line of 5 of the sample of input we need to de-word:
A gosh darn long time ago our darn fathers brought forth on this barking continent a new bum
conceived in liberty and dedicated to the darn proposition that every bum is created equal.

how do i set up the array? using something like string[i] = gosh,darn; ?

i luffs yeww
10-20-2010, 04:51 AM
String[] str = {"gosh", "darn"};

Also, Google. :)

or MSN. panic._@live.com

darkfire23
10-20-2010, 05:13 AM
Just check google, built a construct for the string of bad word array.
It's giving me a syntax error that there is supposed to be name after the = sign in String[] str = ("barking","gosh","darn");

EDIT: Figured it out :)
Thanks for the help guys! i'll let ya know if I run into anymore problems

Method
10-20-2010, 05:15 AM
You need to use curly braces, not parentheses.

The replace method of the String class may come in handy also.

darkfire23
10-20-2010, 05:31 AM
EDIT: used a sequence instead of a char :)

Any way to use .replace to do multiple letters at a time?
ex. "gosh".replace('o';'s';'h', '*')?

i luffs yeww
10-20-2010, 05:45 AM
You could use a loop. Store all the letters in an array of chars and then loop through "gosh".replace(charArr[i], '*');?

moparisthebest
10-20-2010, 08:30 PM
You don't want to use replace, you want to use .replaceAll() right?

Method
10-20-2010, 11:45 PM
You don't want to use replace, you want to use .replaceAll() right?

String.replace() (http://www.coe.uncc.edu/~arwillis/online/docs/jdk-6-docs/api/java/lang/String.html#replace%28java.lang.CharSequence,%20ja va.lang.CharSequence%29) replaces all literal occurrences of the specified CharSequence in the String. String.replaceAll, on the other hand, uses regular expressions to find matches. I don't really see a need for regular expressions in this assignment (unless you want to jam all of the possible bad words in one regular expression String), so String.replace() would probably be easier to use. Both of them replace all occurrences of the specified target, though.