我有一个愚蠢的问题要问你们,因为我在这段代码中遇到了错误。
这是我的第一篇 Stackoverflow 帖子,如果有任何错误,我深表歉意。
我想通过这样的方式返回一个 boolean 值(true 或 false):
VerifyClassUnlocked.verify(account.email,account.password,"PRIEST");
在另一个类(class)。
package realmclient;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
public class VerifyClassUnlocked {
public static boolean verify(String EMAIL, String PASSWORD, String CLASS) {
URL url;
InputStream is = null;
BufferedReader br;
String line;
try {
url = new URL("https://realmofthemadgod.appspot.com/char/list?guid="+EMAIL+"&password="+PASSWORD);
is = url.openStream(); // throws an IOException
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
System.out.println(line);
if (line.contains("<ClassAvailability id=\""+CLASS+"\">unrestricted</ClassAvailability>")){
return true;
}else{
return false;
}
}
} catch (MalformedURLException mue) {
mue.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (is != null) is.close();
} catch (IOException ioe) {
// nothing to see here
}
}
}
}
最佳答案
问题是,如果抛出异常,您将捕获它,然后跳到方法的末尾而不返回值。您希望调用者此时看到什么?
我怀疑您想要让异常在堆栈中传播,或者您想要将return false;
语句移动到方法的底部。
请注意,目前如果您成功读取任何行,那么这就是您要查看的唯一行 - 如果它不是您要查找的行,您将立即返回 false
。这真的是你想做的吗?目前尚不清楚为什么你会有一个只进行一次迭代的 while
循环...
关于Java类和返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21288051/