I have two methods that when prompted by the user will print out n amount of String on m rows, for ex: * 3 3 would print out:
***
***
***
here is the code I have:

public static void writeLine(String c, int n)
{
if(n==0)
System.out.println();
else
System.out.print(c);
writeLine(c, n-1);
}

public static String writeBlock(String c, int m, int n)
{
if (m==0)
System.out.println();
else
writeLine(c, n);
return (writeBlock(c, m-1, n));
}

what is happening is if I put in * 3 3 I get:
***
**************************************... will keep going) the first line is right, it prints out the correct number, but then it just goes to a new line and prints indef.
Can anyone tell me where I went wrong?

Dani AI

Generated

Nice catch by — the symptom there (one correct line then endless output) is the classic result of the recursive call running even after the base case is reached. The immediate fix is to stop the recursion when n hits zero (or keep the recursive call inside the else block), but there are a couple of cleaner alternatives worth using depending on intent and Java version.

If you only need to print m rows of the same n-character string, build the line once and print it m times. In modern Java (11+), String.repeat is concise and fast:

String line = c.repeat(n); // c should be a single-character string like "*"
for (int i = 0; i < m; i++) {
    System.out.println(line);
}

If you need compatibility with older Java or want to avoid allocating the repeated string repeatedly, build it with StringBuilder once and reuse it:

char ch = c.charAt(0);
StringBuilder sb = new StringBuilder(n);
for (int i = 0; i < n; i++) sb.append(ch);
String line = sb.toString();
for (int i = 0; i < m; i++) System.out.println(line);

Extra notes: change writeBlock to return void if it only prints (no need to return a string). Avoid recursion for large m/n to prevent StackOverflowError. Always use braces for if/else blocks to prevent the kind of logic bug that started this thread. These small changes keep code simpler, faster, and easier to reason about than deeply nested recursion for printing output.

Recommended Answers

All 2 Replies

You forgot to return when the writeLine function reaches zero.

Because of that it doesn't act as a sentinel, and writeLine just keeps writing String values to the screen.

Also, it's a good idea to put your code in blocks during a condition.

Here's the modified code. Msg me if there are any errors since I haven't debugged it thoroughly--

public class Testing_Stuff{

	public static void main(String... args){

		writeBlock("Z", 2, 2);

	}

	public static void writeLine(String c, int n)
	{
		if(n==0){
		   System.out.println();
		   return ;
	        }
		else{
			System.out.print(c);
			writeLine(c, n-1);
		}
	}

	public static String writeBlock(String c, int m, int n)
	{
		if (m==0){
			System.out.println();
			return "";
		}
		else{
			writeLine(c, n);
			return (writeBlock(c, m-1, n));
		}
	}
}

You Rock!!! Thanks!

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.