user2920249
user2920249

Reputation: 137

Is this program recursive? If not, how do I make it recursive?

For my programming class, I was told to make a program that uses recursion. I was confused and went to see my friend who was already in the class and he showed me this program. I thought recursion had to use things like r1(x-1), etc. Is it actually recursive? If it's not, how do you make it recursive?

import java.util.*;
import java.io.*;
class ReverseFile
{
    private static Scanner infile;
    public static void main(String[] args) throws IOException
    {
        infile= new Scanner(new File("hw_1.txt"));
        r1();
    }
    public static void r1()
    {
        String s;
        if (infile.hasNextLine())
        {
            s = infile.nextLine();
            r1();
            System.out.println(s);
        }
    }
}

Upvotes: 1

Views: 87

Answers (1)

Simeon Visser
Simeon Visser

Reputation: 122486

It is recursive as r1 calls itself. The fact that no arguments are passed to r1 doesn't matter.

Upvotes: 6

Related Questions