0

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;
    }
}
0

4 Answers 4

2

You don't assign the result of the method to anything. Change your code to:

String errorMessage = checkValue(orderSplit);

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:

String[] orderSplit = null;
orderSplit = order.split(" ");

is unnecessary. You just need

String[] orderSplit = order.split(" ");

And you should never compare Strings with ==. == tests if two variables reference the same String object. You should use the equals() method, which tests if two Strings contain the exact same sequence of characters:

if (errorMessage.equals("")) {
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your help. That solved the problem. Now if I define a bunch of variables within the checkValue method using the array, how can I pass those variables back through to the main method? Thanks.
Basically, you shouldn't. A method should have only one responsibility, and as few side effects as possible. If the responsibility of the method is to check the array, then it shouldn't do anything else. ALso, if that solved the problem, you should upvote and accept the answer. That's how you say thanks here.
Vote up requires 15 reputation, which I don't have, but I check marked it for you. Thanks. So I guess I'll have to re-define the variables all over again, I just didn't want to be redundant.
It's hard to tell without code, but the more local your variables are, and the more stateless your objects are, the better it is in general.
0

You aren't assigning the return value of checkValue anywhere, only returning its parameter. Try changing

String errorMessage = "";
checkValue(orderSplit, errorMessage);

to

String errorMessage = "";
errorMessage = checkValue(orderSplit, errorMessage);

Good luck

2 Comments

There isn't one but it's easier to see this way why the original code didn't behave to OPs expectations. I'm not here to point out everything that might be redundant or not up to scratch with peoples' code but to answer the question posed.
Fair enough. I prefer showing clean code to newbies. I removed my comment because I didn't notice the OP passed the error message as argument to the method, which justifies assigning a value to the variable first (although passing the message is completely unnecessary)
0

java.lang.String is immutable. So whatever you do to errorMessage inside your method will not be visible outside the method, as essentially you are creating new String objects.

You should really check the return value from the method as other answers suggest.

2 Comments

The fact that assigning a new value to errorMessage inside the method won't have any effect on the caller has nothing to do with immutability. It's just that references are passed by value in Java.
@JBNizet I think he's just pointing out that it's literally impossible to change the passed value, no matter how you do it, whereas it's possible to modify a mutable passed object's state.
0

The code above just assigns "" to the errorMessage variable and returns a reference to string "". This in and of itself does nothing.

I am assuming you have some code that assigns a different value to errorMessage in checkVlaue method. There are some caveats to this. Strings are immutable in java. In your message signature the value being passed is a value containing the reference to contents of errorMessage at the time of the call to checkValue. If you attempt to assign another value to this variable inside checkValue and return it you are actually returning a value that is the reference to a different string object. Once you return to the calling method printing errorMessage will print "" because this is the string object that errorMessage in the calling method still points to. If you changed your call to the following:

String errorMessage = checkValue(orderSplit);

You now are assigning the reference value that is returned from checkedValue toerrorMessagein the calling method. This will now print whatever the result ofcheckValue` was.

The net out is to remember that even though most things in java are object references(pointers) that all method signatures are actually pass by value. When you re-assign you are not changing what a pointer points to, but rather are assigning a new pointer to a variable all together.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.