I have a stupid question for you guys, since I get an error with this code.
This is my first Stackoverflow post, so sorry if I did any mistakes.
I want to return a boolean (true or false) by saying this :
VerifyClassUnlocked.verify(account.email,account.password,"PRIEST");
In another 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
}
}
}
}
The problem is that if an exception is thrown, you catch it and then drop to the end of the method without returning a value. What do you want the caller to see at that point?
I suspect you either want to let the exception propagate up the stack, or you want to move your return false;
statement to the bottom of the method.
Note that currently if you read any line successfully, that's the only line you're going to look at - if it's not the one you were looking for, you immediately return false
. Is that really what you want to do? It's not clear why you'd have a while
loop which is only ever going to have a single iteration...
See more on this question at Stackoverflow