0

我制作了以下课程StringToolsNS123,其中包含许多方法

public class StringToolsNS123 {

private String str;

public StringToolsNS123(String s) {
    str = s;
}

public String getString() {
    return str;
}

public void setString(String s) {
    str = s;
}

public String toString()    {
    return str;
}

public int countWord()    {

     String st = " " + str;
     char curr, prev ;
     int count =0;
     for (int i = 0; i <= st.length()-2; i++)
     {
        prev = st.charAt(i);
        curr = st.charAt(i+1);
        if (Character.isLetter(prev) == false && Character.isLetter(curr) == true)
         {
             count++;
         }
     }
     return count;
}
public void firstCaps (String str)
{
    String temp = " " + str ;
    temp = temp.toLowerCase();
    String newStr= "";
    char prev , curr ; 
    for (int loop = 1; loop < temp.length()-1; loop++)
    {
        prev = temp.charAt(loop - 1);
        curr = temp.charAt(loop);
        if (Character.isLetter(prev)== false && Character.isLetter(curr)== true )

        {
            newStr = newStr + Character.toUpperCase(curr);

        }
        else
        {
            newStr = newStr + curr ;
        }

    }
}
public void removeVowels(String str)
{
    String temp = " " + str ;
    temp = temp.toLowerCase();
    String newStr = " ";
    char prev , curr ;
    final String VOWELS = "aeiouAEIOU";

    for (int i = 1; i < temp.length()-1; i++)
    {
        prev = temp.charAt(i-1);
        curr = temp.charAt(i);
        if (Character.isLetter(prev)== false && VOWELS.indexOf(temp.charAt(i))>= 0 || VOWELS.indexOf(temp.charAt(i))== - 1 )
        {
            newStr = newStr + temp.charAt(i);
        }
    }
   str = newStr ;

}
private String encodeWord (String w )
{
    final String VOWELS = "aeiouAEIOU";
    if (VOWELS.indexOf(w.charAt(0))== -1 )

    {
        w = w.substring(1) + w.charAt(0);

    }
     w = w + "ay";
     return  w; 
}
public void pigLatin (String str)
{
    String singleWord, temp = " " + str + " ";
    int begin = 0;
    temp = temp.toLowerCase ();
    String pigStr = " ";
    char prev, curr;
    for (int loop = 1; loop < temp.length ()-1; loop++)
    {
        prev = temp.charAt(loop-1);
        curr = temp.charAt(loop);
        if (Character.isLetter(prev) == false && 
                Character.isLetter(curr) == (true))
        {
            begin = loop;
        }
        if (Character.isLetter(prev) == true && 
                Character.isLetter(curr) == (false))
    {
        singleWord = temp.substring (begin,loop);
        pigStr = pigStr + encodeWord (singleWord) + " "; 
    }
    }
}
public String reverse(String str){
    int strLeng = str.length()-1;
    String reverse = "", temp = "";

    for(int i = 0; i <= strLeng; i++){
        temp += str.charAt(i);
        if((str.charAt(i) == ' ') || (i == strLeng)){
            for(int j = temp.length()-1; j >= 0; j--){
                reverse += temp.charAt(j);
                if((j == 0) && (i != strLeng))
                {
                    reverse += " ";
                }
            }
            temp = "";
        }
    }

    return reverse;
}




}

我遇到的问题是从同一个包中的 JFrame 访问这些方法,我试过了

   StringToolsNS123 str = new StirngToolsNS123 () ; 

但它给出了一个错误,说找不到类

提前致谢!

4

2 回答 2

2

的构造函数StringToolsNS123需要一个String

StringToolsNS123 str = new StringToolsNS123("some string");

您需要从 getFirstCaps 返回一个 String 才能从JFrame该类获得访问权限:

public String firstCaps(String str) {
   ...
   return newStr;
}

通常,像这样的类被实现为带有static方法的实用程序类。在这种情况下,不需要实例,它的用法是:

String firstCaps = MyStringUtilities.getFirstCaps("some string");
于 2013-04-09T16:33:24.973 回答
0

除了你的类的唯一构造函数需要一个字符串(参见 Reimeus 的回答)这一事实之外,你的调用中还有一个错字:

StringToolsNS123 str = new StirngToolsNS123 () ; 

必须

StringToolsNS123 str = new StringToolsNS123 ("some text") ; 

请注意“StringToolsNS1232”中的错字。我不确定您是否从实际代码中复制了该错误,但调用不存在的构造函数会导致“构造函数 StringToolsNS123() 未定义”而不是“找不到类”。

于 2013-04-09T16:52:37.137 回答