2011-05-02 107 views
-1

我真的很想在这里。我试图在字符串中反转这些单词。我正在尝试使用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); 
    } 



} 
+1

你能发布你从编译器得到的错误消息吗? (整个消息,而不仅仅是它的一部分) – coobird 2011-05-02 12:52:42

+1

使用'com.sun.xml.internal'内的任何东西是非常糟糕的做法。我强烈建议你找到解决这个问题的另一种方法(例如使用Apache Commons Lang的'StringUtils') – skaffman 2011-05-02 12:57:02

+0

yep“找不到符号。symbol:method reverseDelimited(java.lang.String.java.lang.String)。location :class com.sun.xml.internals.ws..util.StringUtils“ – Morten 2011-05-02 12:58:07

回答

2

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

+0

另外,应该注意的是,使用'com.sun.xml.internal.ws.util.StringUtils不推荐使用类,因为它是一个内部类(以'com.sun'开头),不属于公共API。最好使用Apache Commons的'StringUtils'等。 – coobird 2011-05-02 12:55:39

+0

@coobird - 谢谢,我以为他用过'Apache Commons Lang'。 – 2011-05-02 12:57:44

+0

谢谢。我会尝试。现在是一个真正的转储问题。我如何使用Apache common和Sun internat?当我尝试使用StringUtils方法时,Netbeans只需要输入Sun internat。 – Morten 2011-05-02 13:03:47