using recursive Java method

import java.util.ArrayList;
public class Count
{
  private String word;
  public Count(String w)
 {  
   word = w;
 }
  public ArrayList<String> getUpper()
{
    ArrayList<String> result = new ArrayList<String>();
    if(word.length()==0)
   {
      result.add(word);
      return result;
   }
    for(int i=0;i< word.length();i++)
{
     if(Character.isUpperCase(word.charAt(i)))
  {
     String shorter = word.substring(i);
     Count  shorterCount = new Count(shorter);
     ArrayList<String> shorterCounter = shorterCount.getUpper();
     for(String s: shorterCounter)
     {
      result.add(s);
     }
  }
}
return result;
}
}

Can someone give me some idea.
thanks very much

Recommended Answers

All 5 Replies

I don't really see your recursive method there? What you need is a base case for your recursive method. In this case the base case could be the end of string, and the base case return the number found...

yea.so could you please give me some example for me ?

I don't really see your recursive method there? What you need is a base case for your recursive method. In this case the base case could be the end of string, and the base case return the number found...

So could you please help me to repair some method?
Given a String parameter, count and return the number of uppercase letters in the string.
Given an int parameter, output its binary representation. For example, the output for parameter 47 would be 101111

this is the question.The result is return the numbers of the UpperCase and ouput the numbers/

Hmm... A very simple one but not the best could be some thing as followed:

public int countUpperRecursively(String str, int pos, int foundCount) {
  // base case
  if (pos==str.length()) { return foundCount; }
  else {

    // Check if the character at str[pos] is upper here.
    // If it is upper, increment foundCount by 1

    // then call recursive and increment pos by 1.
    // Notice that there is a 'return' value here.
    // It will return the value of foundCount from the deepest
    // level to the first level call and back to the caller.
    return countUpperRecursively(str, pos+1, foundCount);
  }
}

// And when you call it from the outside,
// It could be...
//   int total = countUpperRecursively(aStringHere, 0, 0);
// The second argument indicates that your search starts from
// character at position 0.
// The third argument is the total found which is initiated to 0.

Thank you very much .very helpful for me ..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.