java 多变量,从Java方法返回多个变量

Sorry for stupid question...

Could anybody help me with returning two variable from method (I read here

and tried to recode but without result).

public class FileQualityChecker {

EnviroVars vars = new EnviroVars();

public int n = 0;

public int z = 0;

public int m = 0;

String stringStatus;

public void stringLenghtCheck(String pathToCheckedFile)

{

try{

FileInputStream fstream = new FileInputStream(pathToCheckedFile+"\\"+"Test.dat");

// Get the object of DataInputStream

DataInputStream in = new DataInputStream(fstream);

BufferedReader br = new BufferedReader(new InputStreamReader(in));

String strLine;

//Read File Line By Line

while ((strLine = br.readLine()) != null)

{

if (

strLine.length() == vars.strLenghtH ||

strLine.length() == vars.strLenghtCUoriginal ||

strLine.length() == vars.strLenghtCEAoriginal ||

strLine.length() == vars.strLenghtCAoriginal ||

strLine.length() == vars.strLenghtTrailer ||

strLine.length() == vars.strLenghtLastRow

)

{

stringStatus = "ok";

n++;

z++;

}

else

{

stringStatus = "Fail";

n++;

m++;

}

System.out.println (n +" " + strLine.length() +" " + stringStatus);

}

//Close the input stream

in.close();

}

catch

(Exception e){//Catch exception if any

System.err.println("Error: " + e.getMessage());

}

/*How to return m and z from method to use these vars for writing in the file*/

return (m, z);

}

}

I need m and z in another class for write them into file. Thank you.

解决方案

My first question with these sort of issues is, what's the relationship between these two results ?

If they're unrelated, does that point to your method doing two different things ?

If they're related (they appear to be in this case), then wrap them in a custom object. This gives you the chance to add more results later into this object, and you can attach behaviour to this object.

Your other solution is to pass a callback object into this method, so you don't return anything at all, but rather your method then calls a method on this object e.g.

// do some processing

callbackObject.furtherMethod(m, n);

This has the advantage of following the OO principle of getting objects to do things for you, rather than asking them for info and doing it yourself.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值