【发布时间】:2012-08-11 16:29:19
【问题描述】:
对不起,愚蠢的问题... 谁能帮我从方法中返回两个变量(我读过here 并尝试重新编码但没有结果)。
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);
}
}
我需要另一个类中的 m 和 z 来将它们写入文件。谢谢。
【问题讨论】:
-
我会尽可能将您的字段移动为局部变量。
-
如果您正在阅读文本,请不要使用DataInputStream。
-
请不要在您的代码中使用
tab。我讨厌标签...完全不同的设置取决于您最喜欢的 IDE。只需使用空格即可。 -
@maba:天哪。使用标签!当您使用标签时,每个人都可以使用他/她最喜欢的标签宽度设置查看您的文件。使用空格,您可以将您的个人偏好强加给其他人。阅读:lea.verou.me/2012/01/why-tabs-are-clearly-superior。从不,从不使用空格缩进。真的。这是你可以想象的源代码中最烦人的事情。
-
我认为 Tuples 是你可以研究的东西:stackoverflow.com/questions/2670982/using-tuples-in-java