0
public static void displayWords(String line)
{
  // while line has a length greater than 0
  //   find the index of the space.
  //   if there is one,
  //    print the substring from 0 to the space
  //    change line to be the substring from the space to the end
  //   else
  //    print line and then set it equal to the empty string

}
  line = "I love you";
  System.out.printf("\n\n\"%s\" contains the words:\n", line );
  displayWords(line);

Hi, i'm having trouble with the above code, it;s a practice problem but "change line to be the substring from the space to the end" really confuses me. I know how to find the index of space and printing the string from the beginning to the index.

Expected Output:

I
love
you
6
  • 2
    Did you look at the substring() method? Commented Sep 21, 2014 at 4:07
  • I can't add other methods, it's a practice problem where you have to work with the snippet given to you. Commented Sep 21, 2014 at 4:09
  • 2
    he means docs.oracle.com/javase/1.5.0/docs/api/java/lang/… Commented Sep 21, 2014 at 4:10
  • I have to be able to use any other strings like "John went to the store", and the expected output be "John \n went \n to \n the \n store". I could use substring method to print the words, but it wouldn't be applicable to other strings. Commented Sep 21, 2014 at 4:14
  • You can run your string through a scanner. Commented Sep 21, 2014 at 4:17

2 Answers 2

1

SLaks is recommending in his comment you to use the substring() method. Checking the API documentation for String:

public String substring(int beginIndex, int endIndex)

Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.

Examples:

  • "hamburger".substring(4, 8) returns "urge"
  • "smiles".substring(1, 5) returns "mile"

So, consider this:

public static void displayWords(String line) {
    while(line.length() > 0) { // This is the first part (the obvious one)
        /*
           You should do something here that tracks the index of the first space 
           character in the line. I suggest you check the `charAt()` method, and use 
           a for() loop.
           Let's say you find a space at index i
         */
         System.out.println(line.substring(0,i)); // This prints the substring of line
                                                  // that goes from the beginning
                                                  // (index 0) to the position right
                                                  // before the space
         /*
           Finally, here you should assign a new value to line, that value must start
           after the recently found space and end at the end of the line.
           Hint: use substring() again, and remember that line.lengh() will give you
                 the position of the last character of line plus one.
          */
    }
}

You've had enough time... and I'm feeling generous tonight. So here is the solution:

public static void displayWords(String line) {
    int i; // You'll need this
    while(line.length() > 0) { // This is the first part (the obvious one)
        /*
           Here, check each character and if you find a space, break the loop
         */
        for(i = 0; i < line.length(); i++) {
            if(line.charAt(i) == ' ') // If the character at index i is a space ...
                break;                // ... break the for loop
        }
        System.out.println(line.substring(0,i)); // This prints the substring of line
                                                 // that goes from the beginning
                                                 // (index 0) to the position right
                                                 // before the space
        /*
          Here, the new value of line is the substring from the position of the space
          plus one to the end of the line. If i is greater than the length of the line
          then you're done.
         */
        if(i < line.length())
            line = line.substring(i + 1, line.length());
        else
            line = "";
    }
}

After reading your question again, I realised that it should be a recursive solution... so here is the recursive approach:

public static void displayWords(String line) {
    int i;
    for(i = 0; i < line.lengh(); i++) {
        if(line.charAt(i) = ' ')
            break;
    }
    System.out.println(line.substring(0, i);
    if(i < line.lengh())
        line = line.substring(i + 1, line.lengh();
    else
        line = "";
    if(line.lengh() > 0)
        displayWords(line);
}
Sign up to request clarification or add additional context in comments.

Comments

0

Another way of doing it.

public static void displayWords(String line)
{
  Scanner s = new Scanner(line)
  while (s.hasNext())
  {
    System.out.println(s.next())
  }
}

http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html < Use this for reference.

1 Comment

I think that the OP is a beginner, and although this is a very easy way to do exactly what is needed, it's not quite what the OP needs. Sometimes, when learing, it's a good thing to do it "the hard, not quite efficient way" to properly appreciate the "easy, efficient, clean and elegant way"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.