Search code examples
javasdkcognos-bi

use array output in other class


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


Solution

  • 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;
        }
    }