I am having trouble with passing some variables through a method and then having that method return a value. The checkValue method is supposed to look at each of the array items in the orderSplit array and if there is an error with them, return an error message, if not it will return an empty errorMessage. But as of right now the program doesn’t seem to be executing the method at all. Any suggestions?
Here is an example of my code:
public class Foo {
public static void main(String args[]) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String order = null;
try {
order = br.readLine();
} catch (IOException exc) {
System.out.println("ERROR: A Problem has occured");
}
String[] orderSplit = null;
orderSplit = order.split(" ");
String errorMessage = "";
checkValue(orderSplit, errorMessage);
if (errorMessage == "") {
System.out.println("SUCCESS");
}
}
public static String checkValue(String[] orderSplit, String errorMessage) {
// check the ordersplit values
return errorMessage;
}
}
You don’t assign the result of the method to anything. Change your code to:
And not that the checkValue method doesn’t need any error message as argument. It will create one by itself and return it to the caller.
Also, assigning null to a variable you reassign immediately after, like this:
is unnecessary. You just need
And you should never compare Strings with
==.==tests if two variables reference the same String object. You should use theequals()method, which tests if two Strings contain the exact same sequence of characters: