-1

I am really going beserk here. I am trying to reverse the words in a string. I am trying to use StringUtils and have done the import. Why do I get and "Cannot find symbol. Method..."?

Here is my code:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mkrsassignment10;

import com.sun.xml.internal.ws.util.StringUtils;


/**
 *
 * @author mso_
 */
public class Main {
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    //Q1
    //int index;
    String sentence = "is this a sentence or is it not ";

    // 1a
    String[] myStringArray = sentence.split(" "); //Split the sentence by space.
    String wordLongest = myStringArray[0]; //assign longest word to the first word
    for (int i=0; i < sentence.length(); ++i) { //loop through the sentence to find the longest word
      if(wordLongest.length() < myStringArray.length)
        wordLongest = myStringArray[i];
      else
    break;
    }
    System.out.println("The word is: " + "'"+ wordLongest +"'" + " and it is " + wordLongest.length() + " characters long");

    // 1b
    //for (int i = 0; i < myStringArray.length; i++) {
    //    myStringArray.toString();
    //    else
    //    break;

    // 1c
    String reversed = StringUtils.reverseDelimited(sentence, " "); // <--- here is the error
    System.out.println("Reversed words:" + reversed);

    // 1d
    sentence = new StringBuffer(sentence).reverse().toString();
    System.out.println("Reversed String : " + sentence);
  }



}
jzd
  • 23,473
  • 9
  • 54
  • 76
Morten
  • 47
  • 1
  • 6
  • 12
  • 1
    Could you post the error message that you're getting from the compiler? (The entire message rather than only part of it.) – coobird May 02 '11 at 12:52
  • 1
    It's very bad practice to use anything from inside `com.sun.xml.internal`. I strongly advise you to find another approach to solving that particular problem (e.g. use `StringUtils` from Apache Commons Lang) – skaffman May 02 '11 at 12:57
  • yep "cannot find symbol. symbol: method reverseDelimited(java.lang.String.java.lang.String). location: class com.sun.xml.internals.ws..util.StringUtils" – Morten May 02 '11 at 12:58

1 Answers1

2

I suppose you use Apache Commons Lang. It wants a char for the second argument not a String. Try this: StringUtils.reverseDelimited(sentence, ' ');

Petar Minchev
  • 46,889
  • 11
  • 103
  • 119
  • Also, it should be noted that the use of `com.sun.xml.internal.ws.util.StringUtils` class is not recommended as it is an internal class (starts with `com.sun`) that's not part of the public API. It would be better to use `StringUtils` from Apache Commons and such. – coobird May 02 '11 at 12:55
  • @coobird - Thanks, I thought he used `Apache Commons Lang`. – Petar Minchev May 02 '11 at 12:57
  • Thanks. I will try. And now a really dump question. How do I use Apache common and Sun internat? Netbeans only askes to import Sun internat when I try to use the StringUtils method. – Morten May 02 '11 at 13:03
  • @Morten - Because you haven't downloaded and added `apache commons lang` to your project. It is an external library. – Petar Minchev May 02 '11 at 13:04