1

When I try to print myWords in a separate class, all I get a a blank statement. I'm pretty sure the code is right but I just can't get it to print the words that I want. Please help!

public String[] multiLetter (int n, char included)
//public String[] multiLetter(int n, char included)
//returns an array of words with at least m occurrences
//of the letter included

{   
    for (int i = 0 ; i < words.size() ; i++)    
    {
        int repeatCounter = 0;
        for (int j = 0 ; j < words.get(i).length(); j ++)
        {   
            if (included == words.get(i).charAt(j))
            {
                repeatCounter ++;
            }
        }//closes inner for loop 

        if (repeatCounter >= n)
        {
            myWords.add(words.get(i));
        }
    }

    String [] array = new String [myWords.size()];
    return myWords.toArray(array);              
}   
4
  • 2
    You never print anything. Commented Apr 20, 2014 at 23:42
  • 1
    You have left out enough of the relevant info that we can't help you. Commented Apr 20, 2014 at 23:44
  • We need to see the class where you attempt to print the array Commented Apr 20, 2014 at 23:45
  • How is myWords defined? You can add as first line of method the following code: List<String> myWords = new ArrayList<String>();. It is enough when myWords is local variable. Commented Apr 21, 2014 at 1:23

1 Answer 1

1

First: using the way you coded, you have to receive a list of String as a paramater. Second: you have to create you myWords variable. I think your code should be ok that way:

public String[] multiLetter (int n, char included, List<String> words)
//public String[] multiLetter(int n, char included)
//returns an array of words with at least m occurrences
//of the letter included
{   
    List<String> myWords = new ArrayList<String>();
    for (int i = 0 ; i < words.size() ; i++)    
    {
        int repeatCounter = 0;
        for (int j = 0 ; j < words.get(i).length(); j ++)
        {   
            if (included == words.get(i).charAt(j))
            {
                repeatCounter++;
            }
        }//closes inner for loop 

        if (repeatCounter >= n)
        {
            myWords.add(words.get(i));
        }
    }
    return (String[])myWords.toArray();             
}  
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.