Results 1 to 6 of 6

Thread: Need Help

  1. #1
    ing
    ing is offline Member
    Join Date
    Apr 2008
    Posts
    3
    Rep Power
    0

    Default Need Help

    I'm building a program that allows a user to select a word from a sentence and replace it with a word, which a user also enters. i also want to all in-case sensitive.

    So for example:

    a user enter the following
    sentence: Hello my name is Peter Peters
    wordSelect: peters
    replacementWord: Pan

    Result: Hello my name is Peter Pan

    can anyone help?

  2. #2
    Join Date
    Apr 2008
    Location
    Fort Wayne, Indiana
    Posts
    62
    Rep Power
    0

    Default

    This should work. All of my tests worked. Let me know if this is what you need. You'll just have to add in the user input.

    Java Code:
      static String repWord(String sentence, String wordSelect, String replacementWord) {
        String temp;
        String[] splitSentence;
        temp = sentence.toLowerCase();
        wordSelect = wordSelect.toLowerCase();
        splitSentence = temp.split(wordSelect);
        int count = 0;
        String finalSentence = "";
        for (String x : splitSentence) {
          finalSentence += sentence.substring(count, count + x.length()) + replacementWord;
          count += x.length() + wordSelect.length();
        }
        if (temp.lastIndexOf(wordSelect) != temp.length() - wordSelect.length()) {
          finalSentence = finalSentence.substring(0, finalSentence.length() - replacementWord.length());
        }
        return finalSentence;
      }
    
      public static void main(String[] args) {
    
        String sentence = "Hello my name is Peter Peters";
        String wordSelect = "peters";
        String replacementWord = "Pan";
        String output;
        output = repWord(sentence, wordSelect, replacementWord);
        System.out.println(output);
    
      }

  3. #3
    ing
    ing is offline Member
    Join Date
    Apr 2008
    Posts
    3
    Rep Power
    0

    Default

    Yeah thanks, it great, thanks. Just one question is it possiible to not have the "static String repWord()".

  4. #4
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    319
    Rep Power
    10

    Default

    Possible....


    just make an instance of class in your main method and call method on that instance.



    sanjeev

  5. #5
    ing
    ing is offline Member
    Join Date
    Apr 2008
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by ing View Post
    I'm building a program that allows a user to select a word from a sentence and replace it with a word, which a user also enters. i also want to all in-case sensitive.

    So for example:

    a user enter the following
    sentence: Hello my name is Peter Peters
    wordSelect: peters
    replacementWord: Pan

    Result: Hello my name is Peter Pan

    can anyone help?
    How do make it so that it doesnt replace words that don't match. for example:

    sentence: Hello my name is Peter Peters. Peters's
    wordSelect: peters
    replacementWord: Pan

    Result: Hello my name is Peter Pan. Peters's

    Thanks

  6. #6
    Join Date
    Apr 2008
    Location
    Fort Wayne, Indiana
    Posts
    62
    Rep Power
    0

    Default

    You could just make it so that whatever word you go to search for you add a space before and after it. But then if the word comes before a comma or period it wont change it. You would have to make a case statement and have it grab the letter after the word each time and if it is a space, comma, or period allow it, but if it is anything else don't touch it. With this extra overhead it may be more beneficial to add regular expressions into it. Goodluck.

Posting Permissions

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