aspose file tools*
The moose likes Beginning Java and the fly likes Recursive method and an array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Recursive method and an array" Watch "Recursive method and an array" New topic
Author

Recursive method and an array

John Lockheart
Ranch Hand

Joined: Oct 13, 2006
Posts: 115
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?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
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

Joined: Oct 13, 2006
Posts: 115
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

Joined: Jun 09, 2003
Posts: 4632
> 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

Joined: Oct 13, 2006
Posts: 115
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

Joined: Jun 09, 2003
Posts: 4632
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

Joined: Oct 13, 2006
Posts: 115
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

Joined: Jun 09, 2003
Posts: 4632
> 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

Joined: Oct 13, 2006
Posts: 115
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

Joined: Jun 09, 2003
Posts: 4632
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

Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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 ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
John Lockheart
Ranch Hand

Joined: Oct 13, 2006
Posts: 115
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

Joined: Jun 09, 2003
Posts: 4632
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)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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

Joined: Oct 13, 2006
Posts: 115
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!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Recursive method and an array
 
Similar Threads
Array elements
Recursion vs Nested loops.
[B&S] some questions about "Required Interface"
Enhanced For Loop
Counting uppercase? Part 3