[Java] Can someone help me write this code?

I am currently doing a chatbot assignment in Java and I need help figuring out what one of the instructions in the assignment means. Can someone please help?

The instructions:

** Modify the code below to print the values of psn , before , and after right after the comment on line 100 in the findKeyword method below.**

My code so far:

/**
 * A program to carry on conversations with a human user.
 * This version:
 * <ul><li>
 *    Uses advanced search for keywords
 * </li></ul>
 *
 * @author Laurie White
 * @version April 2012
 */
public class Magpie3
{
       /**
        * Get a default greeting
        *
        * @return a greeting
        */
       public String getGreeting()
       {
          return "Hello, let's talk.";
       }

       /**
        * Gives a response to a user statement
        *
        * @param statement
        *            the user statement
        * @return a response based on the rules given
        */
       public String getResponse(String statement)
       {
          String response = "";
              if (statement.length() == 0)
              {
                 response = "Say something, please.";
              }
              else if (findKeyword(statement, "no") >= 0)
              {
                     response = "Why so negative?";
              }
              else if (findKeyword(statement, "mother") >= 0
                             || findKeyword(statement, "father") >= 0
                             || findKeyword(statement, "sister") >= 0
                             || findKeyword(statement, "brother") >= 0)
              {
                     response = "Tell me more about your family.";
              }
              else
              {
                     response = getRandomResponse();
              }
              return response;
       }

       /**
        * Search for one word in phrase. The search is not case
        * sensitive. This method will check that the given goal
        * is not a substring of a longer string (so, for
        * example, "I know" does not contain "no").
        *
        * @param statement
        *            the string to search
        * @param goal
        *            the string to search for
        * @param startPos
        *            the character of the string to begin the
        *            search at
        * @return the index of the first occurrence of goal in
        *         statement or -1 if it's not found
        */
       private int findKeyword(String statement, String goal,
                     int startPos)
       {
          String phrase = statement.trim();
              // The only change to incorporate the startPos is in
              // the line below
              int psn = phrase.toLowerCase().indexOf(
                             goal.toLowerCase(), startPos);

              // Refinement--make sure the goal isn't part of a
              // word
              while (psn >= 0)
              {
                     // Find the string of length 1 before and after
                     // the word
                     String before = " ", after = " ";
                     if (psn > 0)
                     {
                             before = phrase.substring(psn - 1, psn)
                                             .toLowerCase();
                     }
                     if (psn + goal.length() < phrase.length())
                     {
                             after = phrase.substring(
                                             psn + goal.length(),
                                             psn + goal.length() + 1)
                                             .toLowerCase();
                     }

         /* ( LINE 100) determine the values of psn, before, and after at this point */

                     // If before and after aren't letters, we've
                     // found the word
                     if (((before.compareTo("a") < 0) || (before
                                     .compareTo("z") > 0)) // before is not a
                                                                                     // letter
                                     && ((after.compareTo("a") < 0) || (after
                                                     .compareTo("z") > 0)))
                     {
                             return psn;
                     }

                     // The last position didn't work, so let's find
                     // the next, if there is one.
                     psn = phrase.indexOf(goal.toLowerCase(),
                                     psn + 1);

              }

             return -1;
       }

       /**
        * Search for one word in phrase. The search is not case
        * sensitive. This method will check that the given goal
        * is not a substring of a longer string (so, for
        * example, "I know" does not contain "no"). The search
        * begins at the beginning of the string.
        *
        * @param statement
        *            the string to search
        * @param goal
        *            the string to search for
        * @return the index of the first occurrence of goal in
        *         statement or -1 if it's not found
        */
       private int findKeyword(String statement, String goal)
       {
              return findKeyword(statement, goal, 0);
       }

       /**
        * Pick a default response to use if nothing else fits.
        *
        * @return a non-committal string
        */
       private String getRandomResponse()
       {
              final int NUMBER_OF_RESPONSES = 4;
              double r = Math.random();
              int whichResponse = (int) (r * NUMBER_OF_RESPONSES);
              String response = "";

              if (whichResponse == 0)
              {
                     response = "Interesting, tell me more.";
              }
              else if (whichResponse == 1)
              {
                     response = "Hmmm.";
              }
              else if (whichResponse == 2)
              {
                     response = "Do you really think so?";
              }
              else if (whichResponse == 3)
              {
                     response = "You don't say.";
              }

              return response;
       }

       public static void main(String[] args)
       {
             Magpie3 maggie = new Magpie3();

             maggie.findKeyword("yesterday is today's day before.", "day", 0);
             maggie.findKeyword("She's my sister", "sister", 0);
             maggie.findKeyword("Brother Tom is helpful", "brother", 0);
             maggie.findKeyword("I can't catch wild cats.", "cat", 0);
             maggie.findKeyword("I know nothing about snow plows.", "no", 0);

       }

}

We won’t do your homework for you, but we can help you as you work on your homework.

I still don’t know what to do.

Would I write a print statement?

Like this for example:

  1. System.out.println(psn);

You need to print the requested variables right after this comment.

So just System.out.println(psn);

This is C++? That sounds right. If you add that, do the values print out when you run it?

I’m coding in Java and I wrote System.out.prinltn(psn); after Line 100 and got this output:

Output:

6
15
21
9
0
8
19
3
7
22

I don’t know if these are the right values. I think that I also need to write System.out.println(before); and System.out.println(after):

The instructions seem to want you to print out three variables there.

Yeah so I wrote:

         System.out.println(psn);
         System.out.println(before);
         System.out.println(after);

And got this as an output:

6
r

15
o

21

9

0

8

c
19

s
3
k
w
7

t
22
s
w

Can you please help with another part of the assignment? I am not sure what to do on this part either.

Instructions:
Read the description of indexOf(String str, int fromIndex) . Add lines to StringExplorer that illustrate how this version of indexOf differs from the one with one parameter.

My code so far:

/**
 * A program to allow students to try out different
 * String methods.
 * @author Laurie White
 * @version April 2012
 */
public class StringExplorer
{

   public static void main(String[] args)
       {
          String sample = "The quick brown fox jumped over the lazy dog.";

              //  Demonstrate the indexOf method.
              int position = sample.indexOf("quick");
              System.out.println ("sample.indexOf(\"quick\") = " + position);
       
              // IndexOf Behaves as Specified Code
             int notFoundPsn = sample.indexOf("slow");
             System.out.println("sample.indexOf(\"slow\") = " + notFoundPsn); // Returns -1 because value in string is not found
       
              //  Demonstrate the toLowerCase method.
              String lowerCase = sample.toLowerCase();
              System.out.println ("sample.toLowerCase() = " + lowerCase);
              System.out.println ("After toLowerCase(), sample = " + sample);

              //  Try other methods here:
       
              // Upper Case String Method
              String upperCase = sample.toUpperCase();
              System.out.println("sample.toUpperCase() = " + upperCase);
       
              // Compare To Method (int)
              System.out.println(sample.compareTo("brown"));
              
              // Equals Method
              System.out.println(sample.equals("The quick brown fox jumped over the lazy dog."));
       
              // Compare To Ignore Case Method
               String sampleOne = "The quick brown fox jumped over the lazy dog.";
               String sampleTwo = "The QUICK brown fox jumped over the lazy dog.";
               System.out.println(sampleOne.compareToIgnoreCase(sampleTwo));

             
      }
}