1

I am 16 and trying to learn Java, I have a paper that my uncle gave me that has things to do in Java. One of these things is too write and execute a program that will accept an extended message as a string such as

Each time she saw the painting, she was happy

and replace the word she with the word he.

Each time he saw the painting, he was happy.

This part is simple, but he wants me to be able to take any form of she and replace it we he like (she to he, She to He, she? to he?, she. to he., she' to he' and so on). Can someone help me make a program to accomplish this.

I have this

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Write Sentence");
    String original = keyboard.nextLine();
    String changeWord = "he";
    String modified = original.replaceAll("she", changeWord);
    System.out.println(modified);
}

If this isn't the right site to find answers like this, can you redirect me to a site that answers such questions?

7
  • 1
    Please make sure that your question title has some indication of the content of your question. Commented Mar 7, 2016 at 17:25
  • change whatever's entered to a single case, lower or upper and check if the word/sentence contains 'she' Commented Mar 7, 2016 at 17:30
  • 1
    Have you tried testing your program with a string containing all the example cases you quoted to see which of them it does/doesn't handle? Commented Mar 7, 2016 at 17:30
  • I have tried putting them into one string, but it will only correct the lowercase "she" to "he" it will not change "she'll" to "he'll" or "She" to "He." Thanks in advanced for the help! Commented Mar 7, 2016 at 17:36
  • the first should work. For the second there are several ways, the easiest would be to replace "She" with "He" (but this wont consider "sHe" and so on). Another possibility in this case would be to look for she (ignoring the case) and deleting the first letter :-) Commented Mar 7, 2016 at 17:39

2 Answers 2

1

The best way to do this is with regular expressions (regex). Regex allow you to match patterns or classes of words so you can deal with general cases. Consider the cases you have already listed:

(she to he, She to He, she? to he?, she. to he., she' to he' and so on)

What is common between these cases? Can you think of some general rule(s) that would apply to all such transformations?

But also consider some cases you haven't listed: for example, as you've written it now, your code will change the word "ashes" to "ahes" because "ashes" contains "she." A properly written regex expression allows you to avoid this.

Before delving into regex, try and express, in plain English, a rule or set of rules for what you want to replace and what it should be replaced with.

Then, learn some regex and attempt to apply those rules.

Lastly, try and write some tests (i.e. using JUnit) for various cases so you can see which cases your code is working for and which cases it isn't working for.

Once you have done this, if something still doesn't work, feel free to post a new question here showing us your code and explaining what doesn't work. We'll be happy to help.

Sign up to request clarification or add additional context in comments.

Comments

1

I would recommend this regular expression to solve this. It seems you have to search and replace separately the uppercase S and the lowercase s

String modified = original
    .replaceAll("(she)(\\W)", "he$2")
    .replaceAll("(She)(\\W)", "He$2");

Explanation :

  1. The pattern (she) will match the word she and store it as the first captured group of characters
  2. The pattern (\\W) will match one non alphabetic character (e.g. ', .) and store it as the second captured group of characters
  3. Both of these patterns must match consecutive parts of the input string for replaceAll to replace something.
  4. "he$2" put in the resulting string the word he followed by the second captured group of characters (in our case the group has only one character)

The above means that the regular expression will match a pattern like She'll and replace with He'll, but it will not match a pattern like Sherlock because here She is followed by an alphabetic character r

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.