I have to input a directory in DT, then have Directory recursively return a list of files and folders in that directory.

My problem is that at the moment my program is only returning the first file in the directory and then ending.

I know that I'm doing my recursion method wrong, but I can't seem to fix it. Any help would be very helpful.

import java.io.*;
import java.util.*;


public class Directory
{
	private String s;
	private int i;

	public Directory()
	{
		String s = "";
	}

	public Directory(String s)
	{

		this.s = s;

	}

	public String recursiveMethod(){

		File p = new File(s);
		String[] filesInDir = p.list();
		String k = filesInDir[i];
		i++;

		 if (filesInDir == null) return "";
		 if (filesInDir != null) return k;

		 recursiveMethod();

		 return k;

	}
}

Tester

import java.io.*;
import java.util.*;


public class DT
{
    public static void main(String args[])
    {

	System.out.println("Enter directory.");

Scanner scan = new Scanner (System.in);
	String s = scan.next();

	Directory p = new Directory(s);
	printStat(p);

    }
private static void printStat(Directory recurse) {

System.out.print(recurse.recursiveMethod() + "\n");

}
}

Recommended Answers

All 9 Replies

Your recursion method doesn't have any parameters therefore it isn't actually recursive. Recursive methods have a base case which ends the program, usually an if statement whereas the else statement which follows it performs some action and then adds the result to itself, for example:

public static void myMethod( int counter )
	{
	 if( counter == 0 )
	 {
		 return;
	 }else{
		   System.out.println(""+counter);
		   myMethod(--counter);
		   return;
	       }
	}

Above the if statement tested the parameter, if it equals zero then exit the method else continue.

Your recursion method doesn't have any parameters therefore it isn't actually recursive. Recursive methods have a base case which ends the program, usually an if statement whereas the else statement which follows it performs some action and then adds the result to itself, for example:

public static void myMethod( int counter )
	{
	 if( counter == 0 )
	 {
		 return;
	 }else{
		   System.out.println(""+counter);
		   myMethod(--counter);
		   return;
	       }
	}

Above the if statement tested the parameter, if it equals zero then exit the method else continue.

public String recursiveMethod(){

		File p = new File(s);
		String[] filesInDir = p.list();
		String k = filesInDir[i];
		i++;

		 if (filesInDir == null){
			 return "";
		 } else {

			  recursiveMethod();
			  return k;
		  }
	}
}

I get an ArrayIndexOutOfBoundsException. Is this even slightly better?

You need parameters. As mentioned above the base case is what exits the method, we basically work towards this. I assume tat the string array filesInDir is an array containing all the files in the folder?

You need parameters. As mentioned above the base case is what exits the method, we basically work towards this. I assume tat the string array filesInDir is an array containing all the files in the folder?

It should, it at least displays the first file in the directory atm, but then as stated above it ends.

Your base case for the recursive function covers all the possible outcomes.
The reason why it only returns the first element is because that one of the
base cases.

//int i will keep up with which element you are evaluating
public String recursionMethod(int i)
{
    File p = new File(s);
    String[] filesInDir = p.list();
    String k = filesInDir[i];

    if(filesInDir == null) return "";
    else if(i < (filesInDir.length-1))
    {
       return k+ "\n" + recursiveMethod(i+1);//Keeps calling the method until the base case is satisfied. By calling the method with (i+1) as a parameter you will continue through each element in the array until arriving the end of the array.
    }
    else return "";//When i reaches the end of the array return an empty string.
}

When you call the method in you main, send 0 in as a parameter.
Ex. recursiveMethod(0);
The \n is also included in the recursiveMethod because you will be return one String that has all of the information.

I hope this helps.
I hope this helps

Try something along these lines:

// File p = new File(s);
// String[] filesInDir = p.list();
// Both outside the method

int position = 0;

public int recursiveMethod( int position )
{
      if( position == filesInDir.length )
      {
          // Stop method
          return;
      } else {
          System.out.println( "File at position "+position+" is: "+filesInDir[position] );
          return recursiveMethod( position+1 );
}

Not saying this exact method will work as I have tested it but something along these lines should do. I commented out the File and String array but you shouldn't do this just remove them from the method.

commented: Thanks for trying to help. +1

Your base case for the recursive function covers all the possible outcomes.
The reason why it only returns the first element is because that one of the
base cases.

//int i will keep up with which element you are evaluating
public String recursionMethod(int i)
{
    File p = new File(s);
    String[] filesInDir = p.list();
    String k = filesInDir[i];

    if(filesInDir == null) return "";
    else if(i < (filesInDir.length-1))
    {
       return k+ "\n" + recursiveMethod(i+1);//Keeps calling the method until the base case is satisfied. By calling the method with (i+1) as a parameter you will continue through each element in the array until arriving the end of the array.
    }
    else return "";//When i reaches the end of the array return an empty string.
}

When you call the method in you main, send 0 in as a parameter.
Ex. recursiveMethod(0);
The \n is also included in the recursiveMethod because you will be return one String that has all of the information.

I hope this helps.
I hope this helps

I'm getting this

"recursionMethod(int) in Directory cannot be applied to ()
System.out.print("directory = " + temp.recursionMethod() + "\t");"

I had this problem before and just gave up with the route I was going any hints?

Thank you very much so far your help though.

I decided to figure it out myself I appreciate the help but it hasn't gotten me closer.

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.