LuisIsLuis
LuisIsLuis

Reputation: 11

How do I call a variable in another method?

I know that the variable maxreps isn't in the scope of my main method so I wanted it call it by creating an object, but it still isn't able to get maxreps. How could I fix this?

public class LUIS{
    public void james(){
        int current=1;
        int maxreps=1;

        String adriana = "aabbddddnsspkrrgg";
        for(int a=0; a<adriana.length(); a++){
            if(adriana.charAt(a) == adriana.charAt(a+1)){
                current++;
                if(maxreps>=current){
                    maxreps=current;
                }
            }
        }
    }
        public static void main(String[] args){
        LUIS fritz = new LUIS();
        final int drei = fritz.james;

        System.out.println(maxreps);
    }
}

Upvotes: 0

Views: 1416

Answers (2)

davidxxx
davidxxx

Reputation: 131326

1) final int drei = fritz.james; cannot compile. You cannot invoke a method in this way (that is without ()).

2) Besides, the james() method should have a more meaningful name.
This method computes the max series of a same character. So, you could call it computeMaxSeries()

3) And instead being a void method, you could return the max series number.

4) Besides this :

    for (int a = 0; a < adriana.length(); a++) {
        if (adriana.charAt(a) == adriana.charAt(a + 1)) {

will throw a StringIndexOutOfBoundsException as adriana.charAt(a + 1) refers to an index beyond the valid limit of the String length.
You should rather iterate until the last index -1 :

for (int a = 0; a < adriana.length()-1; a++) {

5) At last this is not consistent since you update maxreps by relying on maxreps instead of current :

if(maxreps>=current){
   maxreps=current;
}

You should rather write :

if (current >= maxreps) {
    maxreps = current;
}

So, finally the method would be :

public int computeMaxSeries(){
    int current=1;
    int maxreps=1;

    String adriana = "aabbddddnsspkrrgg";
    for(int a=0; a<adriana.length()-1; a++){
        if(adriana.charAt(a) == adriana.charAt(a+1)){
            current++;
            if (current >= maxreps) {
               maxreps = current;
            }   
        }
    }
   return maxreps;
}

Now you can do :

    final int maxreps = fritz.computeMaxSeries();
    System.out.println(maxreps);

Upvotes: 2

KevinO
KevinO

Reputation: 4403

As you noted, scoping prevents seeing a variable defined in a different scope. You can resolve your particular issue by returning the value

public int james(){  // <-- change from void to an int return
    int current=1;
    int maxreps=1;

    String adriana = "aabbddddnsspkrrgg";
    for(int a=0; a<adriana.length(); a++){
        if(adriana.charAt(a) == adriana.charAt(a+1)){
            current++;
            if(maxreps>=current){
                maxreps=current;
            }
        }
    }

    return maxreps;  // <-- return the value
}

And then in the main method set a variable to the returned value.

Alternatively, you can define it as a class variable, but there are reasons to avoid doing so -- globals are generally bad.

Upvotes: 2

Related Questions