Search code examples
javastringmethodsindexof

Return word specified by the integer


I know I'm missing some things and that's what I really need help with. The code doesn't work in all cases and am looking for help improving/fixing it.

Assignment:

The code I have so far:

public String word(int num, String words)
{
    int l = words.indexOf(" ");
    int r = words.indexOf(" ", l+1);

    for(int i = 3; i <= num; i++){


     l = r;
     r = words.indexOf(" ", l+1);

    //if(i != num)
//  l = r;

    }       
String theword = words.substring(l,r);
    return theword;


}
}

Solution

  • For the first problem, I'll give you two approaches (1. is recommended):

    1. Use the String.split method to split the words up into an array of words, where each element is a word. Instead of one string containing all of the words, such as "hello my name is Michael", it will create an array of the words, like so [hello, my, name, is, Michael] and that way you can use the array to access the words. Very easy:

      public static String word(int num, String words)
      {
          // split words string into array by the spaces
          String[] wordArray = words.split(" "); // or = words.split("\\s+");
      
          // if the number is within the range
          if (num > 0 && num <= wordArray.length) {
              return wordArray[num - 1]; // return the word from the word array
          } else { // the number is not within the range of words
              return null;
          }
      }
      
    2. Only use this if you cannot use arrays! Loop through the word until you have found enough spaces to match the word you want to find:

      public static String word(int num, String words)
      {
          for (int i = 0; i < words.length(); i++) { // every character in words
              if (words.substring(i, i+1).equals(" ")) { // if word is a space
                  num = num - 1; // you've found the next word, so subtract 1 (number of words left is remaining)
              }
              if (num == 1) { // found all words
                  // return this word
                  int lastIndex = i+1;
                  while (lastIndex < words.length()) { // until end of words string
                      if (words.substring(lastIndex, lastIndex+1).equals(" ")) {
                          break;
                      }
                      lastIndex = lastIndex + 1; // not a space so keep moving along the word
                  }
                  /*
                  // or you could use this to find the last index:
                  int lastIndex = words.indexOf(" ", i + 1); // next space after i+1
                  if (lastIndex == -1) { // couldn't find another space
                      lastIndex = words.length(); // so just make it the last letter in words
                  }*/
                  if (words.substring(i, i+1).equals(" ")) { // not the first word
                      return words.substring(i+1, lastIndex);
                  } else {
                      return words.substring(i, lastIndex);
                  }
              }
          }
          return null; // didn't find word
      }
      

    As for the second problem, just iterate backwards through the string and add each letter to a new string. You add each letter from the original string to a new string, but just back to front. And you can use String.toUpperCase() to convert the string to upper case. Something like this:

    public static String reverse(String str) {
        String reversedString = ""; // this will be the reversed string
    
        // for every character started at the END of the string
        for (int i = str.length() - 1; i > -1; i--) {
            // add it to the reverse string
            reversedString += str.substring(i, i+1);
        }
        return reversedString.toUpperCase(); // return it in upper case
    }