• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Recursive method and an array

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have no clue where to begin. I have a Array of Strings, a miniature written program, where each line is a value inside the array. I have to write a recursive method which indents each index value of the array, some more than others. The first value..line of code(index[0]) not at all, then the second value(index[1]) by three spaces, other lines are the same, while some other lines need further indentation. Can anyone help?
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the recursive argument would be the index number of the array
recursion would stop/return when the number equals array.length -1
in the method:
check argument number:


have a go - if you get stuck, post what you've tried

[edit] add code tags for spacing
[ January 16, 2007: Message edited by: Michael Dunn ]
 
John Lockheart
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's my code, I got stuck...The parameter passed to the method "indent" is a string array with actual code for each value. If there is a bracket '{' at the end of the code[x], the next line, code[x+1] needs to be indented. So the counter variable needs to always increase, and i get an error saying index out of range. And each value of the array needs to be printed to screen with the proper indents. This is what I have so far...


 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> tmpStr1.charAt(tmpStr1.length())

charAt() is 0-based
tmpStr1.length() is 1-based

so, the last character should be
tmpStr1.charAt(tmpStr1.length()-1)

test by doing
String tmpStr1 = "12345";
System.out.println(tmpStr1.charAt(tmpStr1.length()-1));
 
John Lockheart
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya i noticed that, thanks, I changed a couple vaules to this code here. The problem is that I have no clue how to recursively call the method, so each index value of the array prints (without using a for loop, whether inside main or its own method). I need to recursively call the method, and have counter variable keep increasing. I'm not even sure if the way I put the methods together is a good way. However i also need counter to stop increasing when it reaches the arrays last index value and stop the program. Any thoughts?

 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public static String indent2(String[] code, int counter, String spaces){
change to
public static String[] indent2(String[] code, int counter, int spaces){
//return value is String[], and spaces is a quantity

String[] array = {someStringArray};
array = indent2(array,0,0);

in indent2:
1)
check if code[counter].charAt(0) is a '}'
if it is, spaces -= 2 (or whatever indent value you want)
2)
create a String pad which is a String of spaces equal to the number in 'spaces'
3)
code[counter] = pad+code[counter];
4)
check if code[counter]'s last character is a '{', if so, spaces += 2
5)
finally the recursion, with check (or finish/return)
if(counter < code.length-1) indent(code,counter+1,spaces);//recursion
return code;//return the array
 
John Lockheart
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Won't that just return the whole array, and then wouldn't I have to use a loop to cycle through each index value in order to print it to screen? And do you mean I should only be using indent2 and delete all the code from the first indent method? I also get the error, "required variable, found value" at these two lines of code when I try and compile it.

 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> Won't that just return the whole array,

yes - each element is modified/indented (where required) as the method
recursively gets to that element (counter+1). At the end, the modified array
is returned and assigned to the original array. Alternative is to give the
array the scope to be 'seen' by indent2(), with arguments only of counter and
spaces, and return zip (void)


> and then wouldn't I have to use a loop to cycle through each index value in
> order to print it to screen?

as you would do normally


> And do you mean I should only be using indent2 and delete all the code
> from the first indent method?

yes - if the exercise is recursion, you only need the one method


> I also get the error, "required variable, found value" at these two lines
> of code when I try and compile it.

perhaps the most common error
if(code[counter].charAt(0) = '}')

= is assignment
== is comparison
 
John Lockheart
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So there's no way to print the contents of an array recursively? You're always going to need a loop to cycle through the index values and print them? how do I print the contents of the new array created with the indent method. and how do I call the indent method, do I use indent(code,0,0)? Sorry, I thought I had this type of stuff down, but all my other code I wrote with recursion was easier for me. Thanks for all your help.

I re-wrote all my code as follows:

 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
see if you can follow the comments in line

note: you will also need to check array element is not null, or empty
before any of the code[counter].charAt() - you have a couple of empty
strings in your array

 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With recursion you should never need to reduce the space count - just return and use the shorter space count at the prior recursion depth. See if this makes sense ...

It would be an interesting exercise to build a string intead of printing, and then print the whole string at the end.

I wonder if I'd like it better without returning the close brace ...

I think I'd like a way to "push" the close brace back before I return from recursion so the prior level could find it and print it. Maybe tomorrow.

Edited to remove some REXX syntax ... I solved this in REXX just to make sure it worked.
[ January 18, 2007: Message edited by: Stan James ]
 
John Lockheart
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stan James - Sorry couldn't follow some of the code, i'm not to experienced with programming and things easily go over my head :S....sorry!
Michael Dunn - Thanks for your time and patience, I appreciate your help and hope to hear back from you on other problems I might have. THANKS!!!

Here's the final code, incase I had an empty string I simply increased the count (count++). One other thing, is a "for loop" the only way to print the contents of an array to screen? There isn't a recursive way to do it? I guess having something like return code[count], and have count always increasing? Just curious...Again THANKS!!!

 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
good to see you have it working, but increasing the counter if length == 0
is not the best way to handle it. if the array has 2 consecutive empty strings
the program will crash.

printing the array recursively is no different to what you already have.

modified code follows

 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here it is with mine ...

We should note that recursion is not really needed in this problem. You could do it by just going through the code array once, add indentation when you find an open brace, subtract indentation when you find close brace. Recursion is almost always entertaining, tho.
[ January 18, 2007: Message edited by: Stan James ]
 
John Lockheart
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cool, thanks for all the help. I cleaned up the program a bit, and added a stub method (making the indent method with three parameters an overloaded one). I'm learning about how to write a program, so you can come along later and implement a diffrent solution without changing much of your program. Everything works great!. THANKS!!!
reply
    Bookmark Topic Watch Topic
  • New Topic