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

Recommended Answers

All 3 Replies

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);

Yep that works too, my example was to show you the method behind the madness :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.