Results 1 to 6 of 6
Thread: Help with a task.
- 01-13-2010, 03:47 PM #1
Member
- Join Date
- Jan 2010
- Posts
- 14
- Rep Power
- 0
Help with a task.
I have the next task. My problem comes when i want to replace the words with '*', iam counting how many * i need in a loop, but when i try to replace them it says that the variable charz is unresolveble:
Java Code:public class Zadacha_6 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub /* * We are given a string containing a list of forbidden * words and a text containing some of these words. Write a program that replaces the forbidden words with asterisks. Example: Words: "Java, JVM, Microsoft" The expected result: Microsoft announced its next generation Java compiler today. It uses advanced parser and special optimizer for the Microsoft JVM. ********* announced its next generation **** compiler today. *It uses advanced parser and special optimizer for the ********* ***. */ String text = "Microsoft announced its next generation Java" + " compiler today. It uses advanced parser and " + "special optimizer for the Microsoft JVM. "; String forbiddenWords = "Java, JVM, Microsoft"; String[] arr = forbiddenWords.split("[ ,]+"); StringBuilder str = new StringBuilder(); str.append(text); String newString = null; for (String word : arr){ System.out.println (word); int lenght = word.length(); int index = text.indexOf(word); for (int i = 0; i <word.length(); i++){ StringBuilder charz = new StringBuilder(); charz.append('*'); System.out.print(charz); } newString = text.replaceAll(word, charz); System.out.println (newString); } } }
-
To simplify, you have this:
You declare charz within the for loop, and so it is visible only within that for loop. Once your code steps out of the loop, *poof*, charz is gone. Solution, declare it before the loop.Java Code:for (int i = 0; i < word.length(); i++) { StringBuilder charz = new StringBuilder(); // *** charz declared here in for loop charz.append('*'); System.out.print(charz); } newString = text.replaceAll(word, charz);
You have another problem too in that charz is a StringBuilder object, and you appear to be trying to use it like a String in the replaceAll method.
- 01-13-2010, 04:54 PM #3
Senior Member
- Join Date
- Oct 2009
- Location
- California,US
- Posts
- 201
- Rep Power
- 4
just make charz as a string, declare it outside the loop and play with it.
It should be pretty easy from then
- 01-14-2010, 09:03 AM #4
Member
- Join Date
- Jan 2010
- Posts
- 14
- Rep Power
- 0
Here i came to this point but its not working correctly? Can someone tell me why?
Java Code:public class Zadacha_6 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub /* * We are given a string containing a list of forbidden * words and a text containing some of these words. Write a program that replaces the forbidden words with asterisks. Example: Words: "Java, JVM, Microsoft" The expected result: Microsoft announced its next generation Java compiler today. It uses advanced parser and special optimizer for the Microsoft JVM. ********* announced its next generation **** compiler today. *It uses advanced parser and special optimizer for the ********* ***. */ String text = "Microsoft announced its next generation Java" + " compiler today. It uses advanced parser and " + "special optimizer for the Microsoft JVM. "; String forbiddenWords = "Java, JVM, Microsoft"; String[] arr = forbiddenWords.split("[ ,]+"); String newString = null; for (String word : arr){ int lenght = word.length(); String chars = ""; for (int i = 0; i <word.length(); i++){ chars += '*'; } newString = text.replaceAll(word, chars); } System.out.println (newString); } }
The result it:
**************** announced its next generation Java compiler today. It uses advanced parser and special optimizer for the **************** JVM.
java isnt replaced
jvm tooLast edited by checho; 01-14-2010 at 09:37 AM.
- 01-14-2010, 11:11 AM #5
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
The replacement is working OK, but you're going back to the original text each time, so the previous replacement is lost. You end up with only the last replacement.
Incidentally, you should be using StringBuilder whenever you concatenate text or characters inside a loop - it's much more efficient. You can convert back to a String whenever you need to using the toString() method. Alternatively, you could forget the loop and just grab the number of '*' you need from a predefined string of them, using subString(..).
- 01-14-2010, 11:29 AM #6
Member
- Join Date
- Jan 2010
- Posts
- 14
- Rep Power
- 0
Solved. Here is the working code in case someone needs it.
Thanks for the help.
Java Code:public class Zadacha_6 { /** * @param args */ public static void main(String[] args) { //raboti /* * We are given a string containing a list of forbidden * words and a text containing some of these words. Write a program that replaces the forbidden words with asterisks. Example: Words: "Java, JVM, Microsoft" The expected result: Microsoft announced its next generation Java compiler today. It uses advanced parser and special optimizer for the Microsoft JVM. ********* announced its next generation **** compiler today. *It uses advanced parser and special optimizer for the ********* ***. */ String text = "Microsoft announced its next generation Java" + " compiler today. It uses advanced parser and " + "special optimizer for the Microsoft JVM. "; String forbiddenWords = "Java, JVM, Microsoft"; String[] arr = forbiddenWords.split("[ ,]+"); String newString = ""; StringBuilder sb = new StringBuilder(); sb.append(text); for (String word : arr){ int lenght = word.length(); int index = sb.indexOf(word); String chars = ""; for (int i = 0; i <word.length(); i++){ chars += '*'; } while (index != -1){ sb.delete(index, index+lenght); sb.insert(index, chars); index = sb.indexOf(word); } } System.out.println (sb.toString()); } }
Similar Threads
-
Java Task
By Sokox in forum NetBeansReplies: 4Last Post: 12-13-2009, 09:57 AM -
java-task.com
By marcellis in forum IntroductionsReplies: 0Last Post: 09-28-2009, 10:46 AM -
Task Scheduler
By fabs in forum Threads and SynchronizationReplies: 3Last Post: 04-21-2009, 11:17 AM -
Get Paid to do this task!
By Zass101 in forum New To JavaReplies: 14Last Post: 04-21-2009, 05:44 AM -
Task Blocks 0.5
By johnt in forum Java SoftwareReplies: 0Last Post: 08-08-2007, 08:43 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks