在其他类中使用数组输出

时间:2016-11-18 16:00:57

标签: java sdk cognos-bi

美好的一天!

我有一个方法,它返回一个报告名称数组

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

我想在其他类中使用数组输出,我需要在其他类的数组中链接方法outpud吗?

方法是:

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

数组在我想要的数组看起来像:

String [] folders = 

我的尝试:

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

返回一个错误:无法从void转换为字符串

给我一​​个解释,以了解我如何与其他类的方法输出相关。

由于

1 个答案:

答案 0 :(得分:1)

问题是你的executeTasks方法实际上并没有返回任何内容(这就是为什么它void),而只是打印到stdout。而不是打印,将名称添加到数组,然后返回它。像这样:

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