문제

This question already has an answer here:

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);
  }



}
도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top