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.

你可能感兴趣的:(java,多变量)