Results 1 to 10 of 10
  1. #1
    bobocheez is offline Member
    Join Date
    Aug 2010
    Posts
    28
    Rep Power
    0

    Default Java help - Replace all if string contains multiple substrings

    So there's a really old guy teaching Java at my college, and I have not learned a thing. He set a 1 day deadline to write something that we don't even know about. The book is worthless as well.

    Here we're inputting a string and searching for 3 words to take out. However, it this only works if all 3 words are inserted. I can't even think straight anymore...after 3 straight hours of staring at my screen.

    Java Code:
    Scanner keyboard = new Scanner(System.in);
    		String v1 = keyboard.nextLine();
    		String dog = "dog";
    		String cat = "cat";
    		String llama = "llama";
    				
    		String result = v1.replaceAll("(?i)dog ", "");
    		String result2 = result.replaceAll("dog ", "");
    		
    		String result3 = result2.replaceAll("(?i)cat ", "");
    		String result4 = result3.replaceAll("cat", "");
    		
    		String result5 = result4.replaceAll("(?i)llama ", "");
    		String result6 = result5.replaceAll("llama ", "");
    		
    		System.out.println(result6);
    This is so similar to PHP but so many different exceptions >.< and tight rules.

    Thanks for your help

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,328
    Rep Power
    25

    Default

    Can you explain in a bit more detail what the problem with your code is?
    Like show the current output and what you'd like it to be.

    For ease of debugging, create a String to feed to the Scanner class so we can work with the same input:
    String Input = "the first line\nthe second line\n the third line";
    Scanner keyboard = new Scanner(Input);

    Or just hardcode the String v1:
    String v1 = "the string to replace with";

  3. #3
    bobocheez is offline Member
    Join Date
    Aug 2010
    Posts
    28
    Rep Power
    0

    Default

    Say someone is on a forum, you want to take out the bad words. In this case, "dog" "cat" and "llama" while disregarding the case.

    For example:
    String v1 = "The dog DOG ate the caT before the LLaMA could get there";
    The output should be: "The ate the before the could get there"

    It should also work if one or two of the variables are excluded. For example: The dog ate the DoG gone caT

    The code above will only work if all of the words are included.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,328
    Rep Power
    25

    Default

    I just ran your code with:
    String v1 = "The dog DOG ate the caT before the sheriff could get there";
    and got output:

    The ate the before the sheriff could get there

    Note: LLaMA was NOT there. It did remove dog and cat.

    Can you explain what your problem is?

  5. #5
    bobocheez is offline Member
    Join Date
    Aug 2010
    Posts
    28
    Rep Power
    0

    Default

    Run this line for v1:
    The DOG ate the Cat before the LLama llama LLAMA
    LLAMA still shows up at the end

  6. #6
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by bobocheez View Post
    So there's a really old guy teaching Java at my college, and I have not learned a thing.
    Sorry but this is not the best excuse we've heard for not learning the material, and we've heard it all before here. You do understand that in school as in life, you and only you are responsible for your education, and the sooner you accept this, the sooner you'll succeed.

    Now for the possible cause of your problem -- almost all your replacement first string parameters in your replaceAll method take a String with a space on the end of it. If there's no space after one of the key words, it won't be recognized. If it is within the parameters of your assignment, I'd get rid of all of those end spaces.

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,328
    Rep Power
    25

    Default

    Your pattern ends with a blank.
    The code works with this:
    "The DOG ate the Cat before the LLama llama LLAMA "

  8. #8
    bobocheez is offline Member
    Join Date
    Aug 2010
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by curmudgeon View Post
    Sorry but this is not the best excuse we've heard for not learning the material, and we've heard it all before here. You do understand that in school as in life, you and only you are responsible for your education, and the sooner you accept this, the sooner you'll succeed.

    Now for the possible cause of your problem -- almost all your replacement first string parameters in your replaceAll method take a String with a space on the end of it. If there's no space after one of the key words, it won't be recognized. If it is within the parameters of your assignment, I'd get rid of all of those end spaces.
    [moderator edit: text deleted due to inappropriate language]

    Quote Originally Posted by Norm View Post
    Your pattern ends with a blank.
    The code works with this:
    "The DOG ate the Cat before the LLama llama LLAMA "
    Thanks
    Last edited by Fubarable; 08-31-2010 at 05:33 AM.

  9. #9
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    [moderator edit: post deleted as it has nothing to do with the original question]
    Last edited by Fubarable; 08-31-2010 at 05:33 AM.

  10. #10
    bobocheez is offline Member
    Join Date
    Aug 2010
    Posts
    28
    Rep Power
    0

    Default

    [moderator edit: post deleted as it has nothing to do with the original question]
    Last edited by Fubarable; 08-31-2010 at 05:34 AM.

Similar Threads

  1. Replace String
    By Raeghin in forum New To Java
    Replies: 1
    Last Post: 07-28-2009, 03:58 PM
  2. replace a string using Text i/o
    By PureAwesomeness in forum New To Java
    Replies: 9
    Last Post: 07-06-2009, 02:38 AM
  3. [SOLVED] how to replace exact string in java
    By pankaj_salwan in forum New To Java
    Replies: 22
    Last Post: 07-08-2008, 09:28 AM
  4. Find and replace ( in a String
    By hamish10101 in forum New To Java
    Replies: 6
    Last Post: 01-17-2008, 05:51 AM
  5. String replace method
    By venkata.tarigopula in forum Advanced Java
    Replies: 1
    Last Post: 07-10-2007, 08:14 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
  •