So basically my assignment is to write a few Java methods that contain arrays in the parameters, and then test each one using a main method within the same class. However, I can't figure out how to invoke the methods... I keep getting a compiler error that states "join(char[]) in Homework cannot be applied to (char,char,char)". Here's one of the methods I'm working on - does anyone have any suggestions? I will greatly appreciate any help!

public class Homework
     public static String join (char[]a)
          String result = "";
          for (int i = 0; i < a.length; i++)
               result = result + " " + a[i];
          } // end for

          return result;

     } // end method

     public static void main (String args [])
           System.out.println("join method test = " + join('a', 'b', 'c'));
      } // end main
} // end class
7 Years
Discussion Span
Last Post by darkagn

Instead of:

System.out.println("join method test = " + join('a', 'b', 'c'));

you want to call join like so:

// build a char array containing the three characters you tried
char[] chars = new char[3];
chars[0] = 'a';
chars[1] = 'b';
chars[2] = 'c';
// pass the array into the call to join method
String result = join(chars);
// print out the result
System.out.println("join method test = " + result);

Thank you very much! I didn't realize it was that different from invoking any other kind of method. I think I'll use a slightly more compact version of the code you gave me though:

char [] z = {'a', 'b', 'c'};
String result1 = join(z);
System.out.println("Results using 'a', 'b', 'c' =  " + results1);
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.