Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 11-14-2011, 08:02 AM PM User | #1 rathrathon New to the CF scene   Join Date: Nov 2011 Posts: 1 Thanks: 0 Thanked 0 Times in 0 Posts Please help me, java recursion problem ok so i got into high school AP CPS and am currently learning java on eimacs.com, but there program is not all too help full when learning, at least for me any ways i was hoping someone could explain some code to me, what it does etc, and then help me with a problem for first the explanation Code: ```public static String reverseString( String s ) { if ( s.length() == 0 ) return ""; String firstChar = s.substring( 0, 1 ); String reverseRest = reverseString( s.substring( 2 ) ); String result = reverseRest + firstChar; return result; } public static void main( String[] args ) { String a = "The sky's the limit!"; System.out.println( reverseString( a ) ); }``` and then the problem i am trying to do is count vowels in a word, using recursion and not using for or while loops my code is Code: ``` public static int countVowels( String s ) { int i = 0; int total = 0; String vowel = s.substring( i,i+1 ); if (s.equals("") ) return 0; if ( vowel == "a" || vowel == "A" || vowel == "e" || vowel == "E" || vowel == "i" || vowel == "I" || vowel == "o" || vowel == "O" || vowel == "u" || vowel == "U" ) total++; i++; if ( i <= s.length () ) { i++; countVowels ( s.substring ( i, i + 1 ) ); } else return total ; }``` which always gives me a missing returns statement, or when i play with the code a stack overflow any help please, i am so far behind in my class, and am so lost... THANK YOU IN ADVANCE, RATHRATHON Last edited by Fou-Lu; 11-14-2011 at 05:58 PM..
 11-14-2011, 06:24 PM PM User | #2 Fou-Lu God Emperor     Join Date: Sep 2002 Location: Saskatoon, Saskatchewan Posts: 16,595 Thanks: 4 Thanked 2,597 Times in 2,566 Posts Recursion always works the same. Keep going until you find a satisfied result, otherwise, try again. With the first example, the check is on the length of the provided string, once it hits 0 there is no more recursion. Examples are hard to do though, so you need to follow a stack trace. With just the word 'The', you would end up with: Code: ```firstChar1 = 'T' SUB firstChar2 = 'e'; SUB NULL``` So the end result would be 'eT'. The answer appears wrong since the recursion only works from the third character on. That should be fetching from substring(1). You never need to work with an increment when dealing with recursion. This is because the parameter should be modified to match only the remaining options. So with a vowel counter you would need Code: ```SUB COUNTVOWELS(STRING input) SET iTotal TO 0 IF input.LENGTH > 0 THEN SET sEvaluate TO input.SUBSTRING(0, 1) IF sEvaluate.ISVOWEL() THEN SET iTotal TO 1 ENDIF ENDIF SET iTotal TO iTotal + (CALL COUNTVOWELS(input.SUBSTRING(1))) RETURN iTotal END SUB``` ISVOWEL can actually be done from either the Character class, or by comparing. This is wrong though: `if ( vowel == "a" || vowel == "A"`. Strings cannot be compared this way. If you stick to string, you need to use vowel.equals("a") or vowel.equalsIgnoreCase("a"). Edit: Sorry, the above is pseudo code. Please implement it and come back if it gives you any trouble. Last edited by Fou-Lu; 11-14-2011 at 06:40 PM..

 Bookmarks

 Tags java, problem, recursion, school

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 04:56 PM.