Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Recursive Function - Getting back to Java - Am I too old for Logic?? 1

Status
Not open for further replies.

eb24

Programmer
Dec 17, 2003
240
US
It's been since the mid 90's since I've worked with Java and would like to get back into it. My work revolves around 90% .NET and SQL Server (Windows). I am using an old manual to practice and just can't seem to figure it out.

Trying this signature: public static String myChar(char z)

Here is the problem:
1) If z = 'A', "A" is returned
2) For other values of z, the return value consists of
3 parts:
- the return value for the previous letter (z - 1),
- followed by the letter z itself, and
- followed by a 2nd copy of the return value for the previous letter.

EX: myChar('C') ==> "ABACABA".

Any assistance will be greatly appreciated.
 
Could you provide a code snippet or be a little more concrete with your question?

Cheers,
Dian
 
It has no error checking or anything but ...
Code:
class test
{  
	public static void main(String args[])
	{
		System.out.println(myChar('D'));
	}

	public static String myChar(char z)
	{
		String strRetVal = "A";
		if (z != 'A')
		{
			char x = z;
			x--;
			String strTemp = myChar(x);
			strRetVal = strTemp + z + strTemp;
		}
		return strRetVal;
	}
}

"If it could have gone wrong earlier and it didn't, it ultimately would have been beneficial for it to have." : Murphy's Ultimate Corollary
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top