Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Arrays and Recursion

An array containting strings of actual written code needs to be formatted. I'm not sure how to even recursively call a method so it prints the contents of an array, let alone format it's text. Not sure if I even put my methods together properly. The code only prints the first index value of the array, no recursion takes place, and the counter variable value isn't increasing. How do I recursively call the method? Can anyone help?

public static void indent(String[] code){

int counter = 0;
String spaces = " ";

System.out.print(indent(code, counter++, spaces));
}

public static String indent(String[] code, int counter, String spaces){

String tmpStr1,
tmpStr2;

if(counter == 0){
return code[counter];
}

tmpStr1 = code[counter - 1];
tmpStr2 = code[counter];

if(tmpStr1.charAt(tmpStr1.length() - 1) == '{' && counter < code.length - 1){
code[counter] = spaces + tmpStr2;
}

return code[counter];

}

}

Comments

  • zibadianzibadian Posts: 6,349
    : An array containting strings of actual written code needs to be formatted. I'm not sure how to even recursively call a method so it prints the contents of an array, let alone format it's text. Not sure if I even put my methods together properly. The code only prints the first index value of the array, no recursion takes place, and the counter variable value isn't increasing. How do I recursively call the method? Can anyone help?
    :
    : public static void indent(String[] code){
    :
    : int counter = 0;
    : String spaces = " ";
    :
    : System.out.print(indent(code, counter++, spaces));
    : }
    :
    : public static String indent(String[] code, int counter, String spaces){
    :
    : String tmpStr1,
    : tmpStr2;
    :
    : if(counter == 0){
    : return code[counter];
    : }
    :
    : tmpStr1 = code[counter - 1];
    : tmpStr2 = code[counter];
    :
    : if(tmpStr1.charAt(tmpStr1.length() - 1) == '{' && counter < code.length - 1){
    : code[counter] = spaces + tmpStr2;
    : }
    :
    : return code[counter];
    :
    : }
    :
    : }
    :
    Recursions and arrays is quite simple. A recursive function calls itself until a certain condition is met. In case of an array the condition is nearly always the end of the array. In pseudo-code it looks something like this:
    [code]
    recurseExample(Array a, int counter) {
    if (counter < a.length) {
    Do something with a[counter]
    recurseExample(a, counter++); // recursiveness
    } else {
    // a[counter] is invalid, since counter >= a.length
    }
    }
    [/code]
    This example "walks" through the array from some point to the end. For example:
    [code]
    recurseExample(a, 2);
    [/code]
    processes the array from the third element to the last.
    The statement "Do something with a[counter]" can be calls to another method, which performs the processing, or it can be code included in the if-then statement.
    Here is a full working example of a recursive function:
    [code]
    int recursiveSum(int[] numbers, int index) {
    int result;
    if (index < numbers.length) {
    result = numbers[index]; // processing
    result = result + recursiveSum(numbers, index++); // recursive call
    } else {
    result = 0
    }
    return result;
    }
    [/code]
  • Here's the code I have so far. It does what it's supposed to do, I did have a bit of help with it. Could I get anyones opinion? Also one question about arrays and recursion. Is it even possible to get rid of a for loop (when it comes to printing index values of an array), and get the values of an array to print to screen using recursion?

    [code]
    import javax.swing.JOptionPane;

    public class Recursion {

    public static void main (String args[]){

    String[] code = {
    "public class Test {",
    "public static void main(String[] args) {",
    "String s = "Countdown: ";",
    "",
    "for (int i = 1; i <= 20; i++) {",
    "System.out.print(i);",
    "if (i < 20) {",
    "System.out.print(",")",
    "}",
    "System.out.print("...")",
    "}",
    "",
    "System.out.println();",
    "}",
    "}"
    };

    for(int i = 0; i < code.length; i++){
    System.out.println(code[i]);
    }

    System.out.println("===");

    code = indent(code,0,0);

    for(int i = 0; i < code.length; i++){
    System.out.println(code[i]);
    }
    }


    public static String[] indent(String[] code, int counter, int spaces){

    String pad = "";

    if(code[counter].length() == 0){
    counter++;
    }

    if(code[counter].charAt(0) == '}'){

    spaces -=3;

    }

    for(int i = 0; i < spaces; i++){

    pad +=" ";

    }

    code[counter] = pad + code[counter];

    if(code[counter].charAt(code[counter].length() - 1) == '{'){

    spaces +=3;

    }

    if(counter < code.length-1) indent(code,counter+1,spaces);
    return code;

    }
    }
    [/code]

    Zibadian,

    I also wanted to thank you for all of your help, i'm not sure if you've realised that you have helped me before, a couple of months ago, many times. I wasn't able to voice my appreciation because I was out of the country for some time. But I really appreciate all the help you have given me, and value your opinion when it comes to code I write. Thanks again, and i'm sure i'll have some posts in the future and hope to hear back from you. Hope you get this.

  • zibadianzibadian Posts: 6,349
    : Here's the code I have so far. It does what it's supposed to do, I did have a bit of help with it. Could I get anyones opinion? Also one question about arrays and recursion. Is it even possible to get rid of a for loop (when it comes to printing index values of an array), and get the values of an array to print to screen using recursion?
    :
    : [code]
    : import javax.swing.JOptionPane;
    :
    : public class Recursion {
    :
    : public static void main (String args[]){
    :
    : String[] code = {
    : "public class Test {",
    : "public static void main(String[] args) {",
    : "String s = "Countdown: ";",
    : "",
    : "for (int i = 1; i <= 20; i++) {",
    : "System.out.print(i);",
    : "if (i < 20) {",
    : "System.out.print(",")",
    : "}",
    : "System.out.print("...")",
    : "}",
    : "",
    : "System.out.println();",
    : "}",
    : "}"
    : };
    :
    : for(int i = 0; i < code.length; i++){
    : System.out.println(code[i]);
    : }
    :
    : System.out.println("===");
    :
    : code = indent(code,0,0);
    :
    : for(int i = 0; i < code.length; i++){
    : System.out.println(code[i]);
    : }
    : }
    :
    :
    : public static String[] indent(String[] code, int counter, int spaces){
    :
    : String pad = "";
    :
    : if(code[counter].length() == 0){
    : counter++;
    : }
    :
    : if(code[counter].charAt(0) == '}'){
    :
    : spaces -=3;
    :
    : }
    :
    : for(int i = 0; i < spaces; i++){
    :
    : pad +=" ";
    :
    : }
    :
    : code[counter] = pad + code[counter];
    :
    : if(code[counter].charAt(code[counter].length() - 1) == '{'){
    :
    : spaces +=3;
    :
    : }
    :
    : if(counter < code.length-1) indent(code,counter+1,spaces);
    : return code;
    :
    : }
    : }
    : [/code]
    :
    : Zibadian,
    :
    : I also wanted to thank you for all of your help, i'm not sure if you've realised that you have helped me before, a couple of months ago, many times. I wasn't able to voice my appreciation because I was out of the country for some time. But I really appreciate all the help you have given me, and value your opinion when it comes to code I write. Thanks again, and i'm sure i'll have some posts in the future and hope to hear back from you. Hope you get this.
    :
    :
    Here's a method, which prints an array recursively:
    [code]
    void recursePrint(String[] stringArray, int index) {
    if (index < stringArray.length) {
    System.out.println(stringArray[index]);
    recursePrint(stringArray, index++);
    }
    }
    [/code]
  • When I implement the method as follows,

    [CODE]
    import javax.swing.JOptionPane;

    public class A1Q3 {

    public static void main (String args[]){

    String[] code = {
    "public class Ick {",
    "public static void main(String[] args) {",
    "String s = "Countdown: ";",
    "",
    "for (int i = 1; i <= 10; i++) {",
    "System.out.print(i);",
    "if (i < 10) {",
    "System.out.print(",")",
    "}",
    "System.out.print("...")",
    "}",
    "",
    "System.out.println();",
    "}",
    "}"
    };

    recursePrint(code,0);

    //for(int i = 0; i < code.length; i++){
    // System.out.println(code[i]);
    //}

    System.out.println("===");

    code = indent(code,0,0);

    recursePrint(code,0);
    //for(int i = 0; i < code.length; i++){
    // System.out.println(code[i]);
    //}
    }


    public static String[] indent(String[] code, int counter, int spaces){

    String pad = "";

    if(code[counter].length() == 0){
    counter++;
    }

    if(code[counter].charAt(0) == '}'){

    spaces -=3;

    }

    for(int i = 0; i < spaces; i++){

    pad +=" ";

    }

    code[counter] = pad + code[counter];

    if(code[counter].charAt(code[counter].length() - 1) == '{'){

    spaces +=3;

    }

    if(counter < code.length-1) indent(code,counter+1,spaces);
    return code;

    }

    public static void recursePrint(String[] stringArray, int index) {

    if (index < stringArray.length - 1) {

    System.out.println(stringArray[index]);

    recursePrint(stringArray, index++); //line 91

    }

    }

    }
    [/CODE]

    It prints the first index value over and over again, then just says "at A1Q3.recursePrint(A1Q3.java : 91)", which means the problems at line 91 where I call the recursion for printing the values. I'm not sure what to do, the code looks right...
  • zibadianzibadian Posts: 6,349
    : When I implement the method as follows,
    :
    : [CODE]
    : import javax.swing.JOptionPane;
    :
    : public class A1Q3 {
    :
    : public static void main (String args[]){
    :
    : String[] code = {
    : "public class Ick {",
    : "public static void main(String[] args) {",
    : "String s = "Countdown: ";",
    : "",
    : "for (int i = 1; i <= 10; i++) {",
    : "System.out.print(i);",
    : "if (i < 10) {",
    : "System.out.print(",")",
    : "}",
    : "System.out.print("...")",
    : "}",
    : "",
    : "System.out.println();",
    : "}",
    : "}"
    : };
    :
    : recursePrint(code,0);
    :
    : //for(int i = 0; i < code.length; i++){
    : // System.out.println(code[i]);
    : //}
    :
    : System.out.println("===");
    :
    : code = indent(code,0,0);
    :
    : recursePrint(code,0);
    : //for(int i = 0; i < code.length; i++){
    : // System.out.println(code[i]);
    : //}
    : }
    :
    :
    : public static String[] indent(String[] code, int counter, int spaces){
    :
    : String pad = "";
    :
    : if(code[counter].length() == 0){
    : counter++;
    : }
    :
    : if(code[counter].charAt(0) == '}'){
    :
    : spaces -=3;
    :
    : }
    :
    : for(int i = 0; i < spaces; i++){
    :
    : pad +=" ";
    :
    : }
    :
    : code[counter] = pad + code[counter];
    :
    : if(code[counter].charAt(code[counter].length() - 1) == '{'){
    :
    : spaces +=3;
    :
    : }
    :
    : if(counter < code.length-1) indent(code,counter+1,spaces);
    : return code;
    :
    : }
    :
    : public static void recursePrint(String[] stringArray, int index) {
    :
    : if (index < stringArray.length - 1) {
    :
    : System.out.println(stringArray[index]);
    :
    : recursePrint(stringArray, index++); //line 91
    :
    : }
    :
    : }
    :
    : }
    : [/CODE]
    :
    : It prints the first index value over and over again, then just says "at A1Q3.recursePrint(A1Q3.java : 91)", which means the problems at line 91 where I call the recursion for printing the values. I'm not sure what to do, the code looks right...
    :
    Try printing the index instead to see if that increases ok. It should then output the numbers 0 to n, where n is the length-2 of the array.
  • [b][red]This message was edited by circuz_phreak at 2007-1-19 12:41:31[/red][/b][hr]
    Got it working, changed it to:

    [CODE]
    public static void recursePrint(Object[] stringArray, int index) {

    System.out.println(stringArray[index]);

    if (index < stringArray.length - 1) {
    recursePrint(stringArray, index + 1);
    }

    [/CODE]

    Program works great, thanks!!!


Sign In or Register to comment.