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?

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));
		}
	}
}
This article has been dead for over six months. Start a new discussion instead.