Anon
Anon

Reputation: 41

Call another method inside the method in Java

I'm doing an exercise about the method and one-dimensional arrays. I created an array under a method. How can I use that array under another method? For example, I determined a few identities in my first method and the second, I will extract some features from those identities using these identities. Let's say that the more times "xyz" are written in this code, I will increase their health by 1.

Upvotes: 0

Views: 1009

Answers (3)

Karanjot Singh
Karanjot Singh

Reputation: 156

well there are two things 1st - As alex mentioned 2nd you can create array in that function

public static String[] population(int n){
    String[] IDs = new String[n];
    String[] letter = {"x","y","z"};
    for (int j=0; j<n; j++){
        IDs[j] = "";
        for (int i=0; i<128; i++){
            Random rand = new Random();
            int w = rand.nextInt(letter.length);
            IDs[j]+=letter[w];
        }
    }
    return IDs;
}

 private static void calculateHealth(int health) {
    String[] Ids = population(health);
    health =0;

    for (int i=2; i<128; i++){
        if (IDs[i-2].equals("X") && IDs[i-1].equals("Y") && IDs[i].equals("Z")) {
            health+=1;
        }
    }
    System.out.println(health);;
}

Upvotes: 0

Aalexander
Aalexander

Reputation: 5004

The problem is that the Array ID's is currently a local variable of your method population

Here some important Points about Variable in scope in Java:

  • In general, a set of curly brackets { } defines a scope.
  • Any variable defined in a class outside of any method can be used by all member methods.
  • In Java we can usually access a variable as long as it was defined within the same set of brackets as the code we are writing or within any curly brackets inside of the curly brackets where the variable was defined.

Scope means that variables are only accessible inside the region they are created.

Here an example to show this behavior

public class Main {
  public static void main(String[] args) {

    // Code here CANNOT use x

    { // This is a block

      // Code here CANNOT use x

      int x = 100;

      // Code here CAN use x
      System.out.println(x);

   } // The block ends here

  // Code here CANNOT use x

  }
}

In your case you can define the id's in a "higher scope" outside of your population method.

public class L {
    public static String[] IDs = new String[n];

        public static void main(String[] args) {
            
            
        
}
        public static void population(int n){
            String[] letter = {"x","y","z"};
            for (int j=0; j<n; j++){
                IDs[j] = "";
                for (int i=0; i<128; i++){
                    Random rand = new Random();
                    int w = rand.nextInt(letter.length);
                    IDs[j]+=letter[w];
                }
            }
        }

         private static void calculateHealth(int health) {
            population(health);
            health =0;

            for (int i=2; i<128; i++){
                if (IDs[i-2].equals("X") && IDs[i-1].equals("Y") && IDs[i].equals("Z")) {
                    health+=1;
                }
            }
            System.out.println(health);;
        }
}

Then you can use it in both of your methods.


Here is another solution for this problem where you don't have to care about scopes. You can just pass to each of the functions an array as parameter.

public class L {

        public static void main(String[] args) {
            
            
        
}
        public static void population(String[] IDs, int n){
            String[] letter = {"x","y","z"};
            for (int j=0; j<n; j++){
                IDs[j] = "";
                for (int i=0; i<128; i++){
                    Random rand = new Random();
                    int w = rand.nextInt(letter.length);
                    IDs[j]+=letter[w];
                }
            }
        }

         private static void calculateHealth(String[] IDs, int health) {
            population(health);
            health =0;

            for (int i=2; i<128; i++){
                if (IDs[i-2].equals("X") && IDs[i-1].equals("Y") && IDs[i].equals("Z")) {
                    health+=1;
                }
            }
            System.out.println(health);;
        }
}

Upvotes: 2

Martin Zeitler
Martin Zeitler

Reputation: 76639

Just declare the String[] outside of the method declaration; setting it's dimension is useless:

String[] IDs = new String[]{};

And you probably don't need these static keywords either.

Upvotes: 0

Related Questions