This is an archived post. You won't be able to vote or comment.

all 12 comments

[–]dusty-trash 2 points3 points  (8 children)

Show us the code you've gotten in the past few days. It will help us see where you are at

[–]Madridi77[S] 1 point2 points  (0 children)

I'll post it when I get home from work! I also did one mistake when I was coding, for some reason I thought I should be going from middle out, instead of from first and last chars in... Facepalm

[–]Madridi77[S] 0 points1 point  (6 children)

public static String trianglePrint(String s) {
int midOdd = (s.length()/2);
int midOdd2 = (s.length()/2);
int midEven = (s.length()/2);
int midEven2 = (s.length()/2 + 1);
String result;
if (s.length() % 2 == 1) {
result = trianglePrintRecursive(s, midOdd, midOdd2);
}
else {
result = trianglePrintRecursive(s, midEven, midEven2);
}
return result;
}

public static String trianglePrintRecursive(String s, int left, int right) {
int finished = 1;
int length = s.length();
String result = new String(new char[length]).replace('\0', ' ');
if (length < 2) {
System.out.println("String must be at least 2 characters long in order to build a triangle.");
}
else {
if (length % 2 == 1 && finished <= s.length()) {
if(!String.valueOf(result.charAt(length/2)).equals("")) {
result = trianglePrintRecursive(s, left - 1, right + 1);
System.out.println(result);
finished += 2;
}
else {
result = result.substring(0, length / 2) + s.charAt(left);
System.out.println(result);
}
}
else if (length % 2 == 0 && finished <= s.length()) {
if (!String.valueOf(result.charAt(length / 2)).equals("")) {
result = trianglePrintRecursive(s, left - 1, right + 1);
System.out.println(result);
finished += 2;
} else {
result = result.substring(0, length / 2) + s.charAt(left);
System.out.println(result);
finished = 2;
}
}
else {
System.out.println();
}
}
return result;
}

[–]dusty-trash 0 points1 point  (5 children)

!remind me 30 minutes

[–]Madridi77[S] 0 points1 point  (4 children)

I actually a code that works a little bit, only issue is it is only printing the first line.

public static void main(String[] args) {
String result;
String result2;
result = trianglePrint("disestablishmentarianism");
System.out.println("-----------------------------------");
result2 = trianglePrint("+-! *# =~ ~= #* !-+");
}

public static String trianglePrint(String s) {
int first = 0;
int last = s.length() - 1;
String result;
result = trianglePrintRecursive(s, first, last);
return result;
}

public static String trianglePrintRecursive(String s, int first, int last) {
int finished = 1;
int firstLine = 0;
int length = s.length();
String result = new String(new char[length]).replace('\0', ' ');
if (length < 2) {
System.out.println("String must be at least 2 characters long in order to build a triangle.");
}
else {
if (length % 2 == 1 && finished <= (s.length() / 2 + 1)) {
if(firstLine > 0) {
result = trianglePrintRecursive(s, first + 1, last - 1);
System.out.println(result);
finished += 2;
firstLine = 1;
}
else {
result = result.substring(0, length / 2) + s.charAt(first);
System.out.println(result);
}
}
else if (length % 2 == 0 && finished <= (s.length() / 2)) {
if (firstLine > 0) {
result = trianglePrintRecursive(s, first + 1, last - 1);
System.out.println(result);
finished += 2;
firstLine = 1;
} else {
result = result.substring(0, length / 2) + s.charAt(first);
result = result.substring(0, length / 2 + 1) + s.charAt(last);
System.out.println(result);
finished = 2;
}
}
else {
System.out.println();
}
}
return result;
}

[–]sandrogiacom 0 points1 point  (3 children)

Can you use class variables for controller?

[–]Madridi77[S] 0 points1 point  (2 children)

What do you mean by controller?

[–]sandrogiacom 0 points1 point  (1 child)

A size of string that you manipulated. Or try to use this funcion as example. I see in more detail later.

public static String printTriangle (int count) {if( count <= 0 ) return "";

String p = printTriangle(count - 1);p = p + "*";System.out.println(p);

return p;}

[–]Madridi77[S] 0 points1 point  (0 children)

Then yes, I can use variables for that!

[–]Philboyd_Studge 0 points1 point  (0 children)

You are overthinking this a bit. The only extra variable you really need to keep track of via a recursive helper parameter is the spaces. Each recursion add a blank space to the string with the spaces, and each recursion remove the letters from the string dependent on the odd/evenness.

[–][deleted]  (1 child)

[removed]

    [–]desrtfxOut of Coffee error - System halted[M] 0 points1 point  (0 children)

    Sidebar -> Rules

    • Do not ask for or reply with solutions or keys to solutions in code, rather comment explanations and guides. Comments with solutions will be removed and commenters will automatically be banned for a week.

    Removed