Returning an array to a method call

I want to call a method in another class, passing a string value and return an array. The class method will look up records matching the passed string value and put them in the array. How do I return that array? I can’t seem to get the method formulated correctly.

public class Main {

     * @param args the command line arguments
    public static void main(String[] args) {
        getSalesAreas testgetSalesAreas = new getSalesAreas();
        String[] salesAreas = new String[]{testgetSalesAreas.getSalesAreasFromSap("0000210072")};

        // report the number of sales areas, or true/false
//        System.out.println("Sales Areas: " + salesAreas[1]);

        int i = 0;

        for (i = 0; i < salesAreas.length; i++) {




public class getSalesAreas {

    public String getSalesAreasFromSap(String custnum) {
        String[] sa;

	// get the values into the array

	return sa; ???


Please let me know what else I need to post or clarify.

Ah yes, you need to define the return type in Java functions.
Sorry, it’s been a while for me :slight_smile:

Seeing as your function returns an array of Strings, the return type “String” is clearly wrong, it should be “String”, like so:

public String[] getSalesAreasFromSap(String custnum) {
  String[] sa;

  // get the values into the array
  return sa;

The method call signature obviously is not correct…

public String getSalesAreasFromSap(String custnum) {

As now I have ‘incompatible types’ error. My guess is that I need a ‘returns’ parameter in there but I’m not sure of the correct syntax.

Or should I create a nested return object? I’m starting to think this is the better option…

Darn, that was too easy, can’t believe I missed that one. No errors (except the JCO stuff, I’m waiting on the middleware layer to be set up). By the way, I guess I’d be considered a Java newbie, I’ve dabbled in it quite a bit but no formal training, still waiting on my company to provide it. For now I’m trying figure stuff out on my own and it can be quite frustrating!

Thank you for your gracious help.


The return is correct, but the assignation is not.

String salesAreas = new String{testgetSalesAreas.getSalesAreasFromSap(“0000210072”)};

should be

String salesAreas = testgetSalesAreas.getSalesAreasFromSap(“0000210072”);