“找不到符号”?

发布于 11-04 17:50 字数 1489 浏览 5 评论 0原文

我真的要发疯了。我正在尝试反转字符串中的单词。我正在尝试使用 StringUtils 并已完成导入。为什么我会收到“找不到符号。方法...”?

这是我的代码:

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



}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少年亿悲伤2024-11-11 17:50:22

我想您使用的是 Apache Commons Lang。它需要一个 char 作为第二个参数,而不是 String
试试这个:StringUtils.reverseDelimited(sentence, ' ');

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文