0

Good day!

I have a method which returns me an array of report names

System.out.println(bc[i].getDefaultName().getValue() 

i want to use array output in other class, how i need to linked method outpud in my array in other class?

Method is:

public class ReoprtSearch {
    public void executeTasks() {
        PropEnum props[] = new PropEnum[] { PropEnum.searchPath, PropEnum.defaultName};
        BaseClass bc[] = null;
        String searchPath = "//report";
    //searchPath for folder - //folder, report - //report, folder and report - //folder | //report 

        try {
            SearchPathMultipleObject spMulti = new SearchPathMultipleObject(searchPath);
            bc = cmService.query(spMulti, props, new Sort[] {}, new QueryOptions());
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }

        if (bc != null) {
            for (int i = 0; i < bc.length; i++) {

                System.out.println(bc[i].getDefaultName().getValue();
            }
        }
    }
}

array in what i want put the array looks like:

String [] folders = 

my trying like:

ReoprtSearch search = new ReoprtSearch();    
String [] folders = {search.executeTasks()};

Returns me an error: cannot convert from void to string

Give me an explanations to understand how i can related to method output from other class.

Thanks

1 Answers1

1

The problem is that your executeTasks method doesn't actually return anything (which is why it's void), and just prints to stdout. Instead of printing, add the names to an array and then return it. Something like this:

public class ReoprtSearch {
    public String[] executeTasks() {
        PropEnum props[] = new PropEnum[] { PropEnum.searchPath, PropEnum.defaultName};
        BaseClass bc[] = null;

        String searchPath = "//report";
    //searchPath for folder - //folder, report - //report, folder and report - //folder | //report 

        try {
            SearchPathMultipleObject spMulti = new SearchPathMultipleObject(searchPath);
            bc = cmService.query(spMulti, props, new Sort[] {}, new QueryOptions());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        if (bc != null) {
            String results[] = new String[bc.length];
            for (int i = 0; i < bc.length; i++) {
                results[i] = bc[i].getDefaultName().getValue();
            }
            return results;
        }
        return null;
    }
}
Alejandro C.
  • 3,771
  • 15
  • 18
  • I don't understand your comment. Did I misinterpret your question? – Alejandro C. Nov 18 '16 at 16:15
  • At first I did not understand the meaning of. You showed the way how I can write the output to an array. I'll try to use it to test and find out – Egor Rogov Nov 18 '16 at 16:22
  • Alejandro, why line `public String[] executeTasks()` in Eclipse say me what: `This method must return a result of type String[]` for return `results;` at the end? We return `string []` by using `return results;` because you declare results as string in line `String results[] = new String[bc.length];` – Egor Rogov Nov 21 '16 at 06:46
  • Edited the response; you need to return null at the end if bc == null – Alejandro C. Nov 21 '16 at 14:41