3
\$\begingroup\$

The following question was taken from Absolute Java 5th ed. by Walter Savitch:

Write a program that starts with a line of text and then outputs that line of text with the first occurrence of "hate" changed to "love" . For example, a possible sample output might be:
The line of text to be changed is:
I hate you.
I have rephrased that line to read:
I love you.

You can assume that the word "hate" occurs in the input. If the word "hate" occurs more than once in the line, your program will replace only the first occurrence of "hate" . Use a defined constant for the string to be changed. To make your program work for another string, you should only need to change the definition of this defined constant.

This is the code that I have written:

 public class Question5 {

    private static final String STRING_TO_BE_CHANGED = "hate";

    public static void main(String[] args) {

        System.out.println("The line to be changed is:");
        System.out.println(originalLine());
        System.out.println("I have rephrased that line to read:");
        System.out.println(newLine());
    }

    private static String originalLine() {
        return "I hate you";
    }

    private static String newLine() {
        return originalLine().replaceFirst(STRING_TO_BE_CHANGED, "love");
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ What you need from us a general code review ? or you want a better version for this program ? \$\endgroup\$ Sep 10, 2014 at 12:12
  • 1
    \$\begingroup\$ @Shree Generally, code reviewing is checking what can be done to improve the code. \$\endgroup\$ Sep 10, 2014 at 12:30

1 Answer 1

7
\$\begingroup\$

This, and your past few questions you have asked, have been progressively getting better.

In particular, the naming of methods and variables in this question is much better. You are suffering from 'Hungarian Notation' in the constant name. There is no need to call it STRING_TO_BE_CHANGED because we know it is a String. A simpler name TO_CHANGE would be fine.

In order to take your programming mindset to 'the next level', you have to start to abstract the functions from the particular use case. How do you write your program to solve a general problem, and apply it to a specific use case?

In this program of yours, the guts of the function newLine() can only solve one problem, changing hate to love, in a single phrase.

What I would like to see is a more general use case. In this problem, you have three Strings:

  1. I hate you
  2. hate
  3. love

The objective is to search the first string for the second, and to replace it with the third.

A general function would look like:

public static String replaceWordInText(String phrase, String search, String replacement) {...}

That function embodies the logic you are trying to do, and it can replace any search with any replacement in any text.

How would this be used to solve the problem you have in the book?

public class Question5 {

    private static final String TO_SEARCH = "I hate you";
    private static final String TO_CHANGE = "hate";
    private static final String REPLACE_WITH = "love";

    public static void main(String[] args) {

        System.out.println("The line to be changed is:");
        System.out.println(STRING_TO_BE_SEARCHED);
        System.out.println("I have rephrased that line to read:");
        String result = replaceWordInText(TO_SEARCH, TO_CHANGE, REPLACE_WITH);
        System.out.println(result);
    }

    public static String replaceWordInText(String phrase, String search, String replacement) {
        ....
    }

}

Now, we have a general function applied to a specific use-case. How would the general function be implemented? Well, you have already solved that using the replaceFirst method

    public static String replaceWordInText(String phrase, String search, String replacement) {
        return phrase.replaceFirst(search, replacement);
    }
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.