Question about and how to use recursion in Java

Sorry for bothering everyone but I have a question regarding java. (I do realize this is a java script forum but there wasn’t a forum for java so I asked here)

The question is:

Write a recursive method that print an input string in a reverse “V” shape
and then a “V” shape as below

Example:
– If the input string is “1234567” which has an odd number of characters,
then the output is:

   4
  3 5
 2   6
1     7
 2   6
  3 5
   4

If the input string is “abcdef” which has an even number of characters,
then the output is:

  cd  
 b   e
a     f
 b   e
  cd  

I’m not sure which one to use here.

  • public static void printVshape (String str, int i)
  • public static void main(String args)

If I start with the first one,
I get to

public static void printVshape (String str, int
i)
{
if(i<0) return;
String outline;

Can someone finish of question using one of the above methods and explain why to me?

(edited to format the text in the matter in which you’re looking for - I think - Dave M)

Hi,

I moved this to “Get Started” and tagged it with “general-dev”.
There will be a new category to catch all programming questions that don’t have their own category, but it’s not there yet.

I think I know what you’re asking for but maybe you should rephrase your question to make it clearer.

This look like a school takst to me :wink:

here is my current output:
0
11
22

3
3

4
4*
5********5
0
0*
1
1
22*
**33
*****4

but I need:

4
3*5
26
1
7
1
7
2
6
3*5
4

import java.util.;
public class Diamond {
void hollow(int s) {
int i,n;
for (n = 0; n < s ; n++ ){
for (i = 0; i < s-n-1 ;i++ ){
System.out.print("
“);
}
for (i = 0; i <= n; i++ ) {
if (i == 0 || i == n){
System.out.print(”" + n + "“);
}
else
System.out.print(”“);
}
System.out.println();
}
for (n = 0; n < s-1; n ++ ){
for (i = 0; i <= n ; i++ ){
System.out.print(”“);
}
for (i = 0; i < s-n-1 ;i++ ) {
if (i == 0 || i == s-n-2) {
System.out.print(”
" + n + “*”);
}
else
System.out.print("
");
}
System.out.println();
}
}
public static void main(String args)
{
System.out.print("Enter Size: ");
Scanner cin = new Scanner(System.in);
int num = cin.nextInt();
Diamond db = new Diamond();

	db.hollow(num);
	System.out.println();
}

}

That’s my current code

can someone explain what I need to do?