简体   繁体   中英

Passing values between methods

I've got method where I read value into variable

public void displayFromExcel(String xlsPath) {
. 
. 
. 
pole[i] = cell.getNumericCellValue();
.
.
pole1[j] = richTextString;

Then I have method where I build a String using StringBuilder

    private void getHenkValues (StringBuilder sb) { 
    sb.append("<ColumnValue name=\"hen_allockey\">" + pole1[j] + "</ColumnValue\">\r\n"
            +"<ColumnValue name=\"hen_percentage\">"+ pole[i] + "</ColumnValue\">\r\n");
}

Then I have method where I write it into file:

protected void jobRun() throws Exception {
sb = new StringBuilder();
getHenkValues(sb);
String epilog1 = sb.toString();

FileOutputStream fos = new FileOutputStream("c:\\test\\osem.xml");
OutputStreamWriter osw = new OutputStreamWriter(fos, Charset.forName("UTF-8"));
osw.write(epilog1);
osw.flush();
osw.close();  
}

And in method main I call the method jobrun .

How can I get the values from pole[i] , pole1[j] from method displayFromExcel to method getHenkValues ?

Your displayFromExcel method need to return them (using a custom class or a collection of some sort, perhaps an array).

Your getHenkValues needs to accept these values as well, you could try something like:

getHenkValues(StringBuilder sb, Object value1, Object value2)

or whatever is relevant for your case.

You could make pole and pole1 private fields of the class in which displayFromExcel, getHenkValues and jobRun are located:

private Object[] pole;
private String[] pole1;

Then you can assign values to these arrays in one method and access them in another.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM