VerzChan
VerzChan

Reputation: 53

Why is this code not displaying the variable I returned in the method?

There is no error in the code, but when I try to call the method I wrote to check the string I input in the main, it's empty

enter image description here

public static void main(String[] args) {

    Main m = new Main();

    m.balancedStringSplit("LLRRLLLRRRRLR");

}

public int balancedStringSplit(String s)
{
    int count = 0;
    int res = 0;

    for(int i = 0; i<s.length(); i++){
        if(s.charAt(i) == 'L')
        {
            count++;
        }
        else if(s.charAt(i) != 'L')
        {
            count--;
        }

        if(count == 0)
        {
            res++;
        }
    }
    return res;


}

Upvotes: 0

Views: 93

Answers (1)

user11809641
user11809641

Reputation: 895

As others pointed out above, you need to DO something with the return value of the method. It needs to be assigned to a variable so that you can then do something with it:

int i = m.balancedStringSplit("LLRRLLLRRRRLR");
System.out.println(i);

Upvotes: 4

Related Questions