• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Java help

mikysee

Senior member
How do I use the join() fuction?
I need to do something like this. It looks right to me but I keep getting an error when compiling.
String[] test = {"a","b","c"};
String t = join('*',test);
It should print out something like
a*b*c
Thanks for any help.
 
public static String join(char del, String[] strings){
String[] test = {"one","two","three"};
String t = join('*',test);
}

The error I get:
Test.java:2: Return required at end of java.lang.String join(char, java.lang.String[]).
public static String join(char del, String[] args){ ^
 
Originally posted by: notfred
Good thing you didn't post what error you were getting, that couldn't possibly be helpful.

Good point....but next time try to cut down the sarcism 😛

"public static String join(char del, String[] strings) { }"

This says the method is "returning" a string - currently ur method isn't returning anything
That's what the error is saying

And looking at your method, it is really creating an infinite loop (if it worked). I don't know if you really meant it that way but you've made ur method recursive by having a join() method inside the join method itself...

This would be how I would write the method:
public static String join (char del, String[] strings) {
String newString = "";
int i = 0;
while (true) {
if (strings.length-1 == i) {break;}
newString = newString + strings + del;
i++;
}
return newString
}

or alternatively, if you wanna use recursion:
public static String join (char del, String[] strings, int i, String newString) {
if (strings.length-1 == i) {break;}
newString = newString +strings +del;
return newString;
}

Dunno if they are right...i suspect there are heaps of bugs in them (it wuz jus from the top of my head) but u get the idea of it....
 
Damn....

Note that where it says:
newString = newString + strings + del;

the strings part is really string[.i]
The forum italicises it when I do it the proper way.....
 
Back
Top