Tyron Sale
Tyron Sale

Reputation: 1

Accessing methods from JFrame

I have made the following class StringToolsNS123 which consists of a number of methods

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




}

The problem I am having is accessing these methods from a JFrame which is in the same package, I tried

   StringToolsNS123 str = new StirngToolsNS123 () ; 

But it gives an error saying class not found?

Thanks in advance!

Upvotes: 0

Views: 193

Answers (2)

Reimeus
Reimeus

Reputation: 159874

The constructor of StringToolsNS123 requires a String

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

You need to return a String from getFirstCaps to get access from the JFrame class:

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

Typically, classes like these are implemented as a utility class with static methods. In that case no instance is required, its usage would be:

String firstCaps = MyStringUtilities.getFirstCaps("some string");

Upvotes: 2

niniel
niniel

Reputation: 1

Apart from the fact that the only constructor of your class requires a String (see Reimeus's answer), there is a typo in your call:

StringToolsNS123 str = new StirngToolsNS123 () ; 

has to be

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

Note the typo in "StringToolsNS1232". I'm not sure if you copied that mistake from your actual code but calling a non-existent constructor would result in 'The constructor StringToolsNS123() is undefined' rather than 'class not found'.

Upvotes: 0

Related Questions