Results 1 to 6 of 6
  1. #1
    checho is offline Member
    Join Date
    Jan 2010
    Posts
    14
    Rep Power
    0

    Default 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);
    	
    		}
    	
    	}
    
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    To simplify, you have this:
    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 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.

    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.

  3. #3
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    5

    Default

    just make charz as a string, declare it outside the loop and play with it.

    It should be pretty easy from then

  4. #4
    checho is offline Member
    Join Date
    Jan 2010
    Posts
    14
    Rep Power
    0

    Default

    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 too
    Last edited by checho; 01-14-2010 at 09:37 AM.

  5. #5
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    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(..).

  6. #6
    checho is offline Member
    Join Date
    Jan 2010
    Posts
    14
    Rep Power
    0

    Default

    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

  1. Java Task
    By Sokox in forum NetBeans
    Replies: 4
    Last Post: 12-13-2009, 09:57 AM
  2. java-task.com
    By marcellis in forum Introductions
    Replies: 0
    Last Post: 09-28-2009, 10:46 AM
  3. Task Scheduler
    By fabs in forum Threads and Synchronization
    Replies: 3
    Last Post: 04-21-2009, 11:17 AM
  4. Get Paid to do this task!
    By Zass101 in forum New To Java
    Replies: 14
    Last Post: 04-21-2009, 05:44 AM
  5. Task Blocks 0.5
    By johnt in forum Java Software
    Replies: 0
    Last Post: 08-08-2007, 08:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •