user2963042
user2963042

Reputation: 15

How would I get it to repeat?

So my assignment says to Write a method called repl that accepts a String and a number of repetitions as parameters and returns the String concatenated that many times. For example, the call repl("hello", 3) should return "hellohellohello". If the number of repetitions is zero or less, the method should return an empty string.

So this is one of the codes I have written.

import java.util.Scanner;

public class Hello {
    public static void main (String [] args){
        Scanner console = new Scanner (System.in);
        System.out.println("Enter the word");
        String word = console.next();
        System.out.println("Enter the number");
        int y = console.nextInt();

            repl(word, y);

   }

  public static String repl(String word, int y) {
        if (y <= 0) {
            return null;
        }else {
            System.out.print(repl(word, y)); //line 21, error is here
        }
    return word;
    }

}

Currently this code is compiling but when I run it, it prints out

at Hello.repl(Hello.java:21)

over and over again.

I have also written a for code that will only print out the word once. I have been working on this for about an hour now and I am still very confused as to how I can make the word repeat y times.

Can someone please help me understand this code?

Upvotes: 1

Views: 1697

Answers (5)

Mike Christensen
Mike Christensen

Reputation: 91608

You need to pass in the decremented value of y:

public static String repl(String word, int y) {
    if (y <= 0) {
        return null;
    } else {
        System.out.print(repl(word, y - 1));
    }

    return word;
}

This way, each iteration of the recursive call will lower the count by 1, ending when it reaches 0.

Note, you'll probably want to return word when y reaches 0, as it needs to be printed one last time:

public static String repl(String word, int y) {
    if (y <= 0) {
        return word;
    } else {
        System.out.print(repl(word, y - 1));
    }

    return word;
}

Example

At this point, notice we return word no matter what, which makes the first if condition unnecessary. Your entire function could be reduced to:

public static String repl(String word, int y) {
    if (y > 0) System.out.print(repl(word, y - 1));

    return word;
}

Of course, this is probably much easier to do with a for loop, but I'm assuming recursion is part of your assignment.

Upvotes: 1

Mengjun
Mengjun

Reputation: 3197

You need to change your code

System.out.print(repl(word, y));

to

System.out.print(repl(word, --y));

In your code, the value y is not changed. so the method repl will be in infinite recursive.

Upvotes: 0

scottr
scottr

Reputation: 65

  1. Decrement y
  2. null is not an empty string. Try replacing it with ''.

Upvotes: 0

Chris Trudeau
Chris Trudeau

Reputation: 1437

Basically this is what you have to do:

    string toPrint = "";
    for (int i=0; i<y; i++)
    {
        toPrint += word;
    }
    System.out.println(toPrint)

This for loop adds the "word" variable to an empty string the required amount of times, and then you just print that variable.

Unless of course you need to use recursion...

Upvotes: 0

goSun
goSun

Reputation: 29

y is not being decremented anywhere inside repl(String word, int y)

Upvotes: 0

Related Questions