-2

I am working on a simple Bingo game that has randomized numbers. My main class (Bingo) is here:(note that I have more code that is not relevant to the problem)`

public class Bingo {
    public static void main (String[] args) {
        RandNum columnB = new RandNum();
        columnB.max = 15;
        columnB.randNum();
        int b1 = columnB.row1;
        int b2 = columnB.row2;
        int b3 = columnB.row3;
        int b4 = columnB.row4;
        int b5 = columnB.row5;

        RandNum columnI = new RandNum();
        columnI.min = 16;
        columnI.max = 30;
        columnI.randNum();
        int i1 = columnI.row1;
        int i2 = columnI.row2;
        int i3 = columnI.row3;
        int i4 = columnI.row4;
        int i5 = columnI.row5;

        RandNum columnN = new RandNum();
        columnN.min = 31;
        columnN.max = 45;
        columnN.randNum();
        int n1 = columnN.row1;
        int n2 = columnN.row2;
        int n3 = columnN.row3;
        int n4 = columnN.row4;
        int n5 = columnN.row5;

        RandNum columnG = new RandNum();
        columnG.min = 46;
        columnG.max = 60;
        columnG.randNum();
        int g1 = columnG.row1;
        int g2 = columnG.row2;
        int g3 = columnG.row3;
        int g4 = columnG.row4;
        int g5 = columnG.row5;

        RandNum columnO = new RandNum();
        columnO.min = 61;
        columnO.max = 75;
        columnO.randNum();
        int o1 = columnO.row1;
        int o2 = columnO.row2;
        int o3 = columnO.row3;
        int o4 = columnO.row4;
        int o5 = columnO.row5;
    }
}

My class that I am using to generate the random numbers:

public class RandNum {
    int min = 1;
    int max;

    public void randNum() {
        Random rn = new Random();

        int row1 = rn.nextInt(max - min + 1) + min;
        int row2 = rn.nextInt(max - min + 1) + min;
        int row3 = rn.nextInt(max - min + 1) + min;
        int row4 = rn.nextInt(max - min + 1) + min;
        int row5 = rn.nextInt(max - min + 1) + min;
    }
}

Now the issue is that I am trying to set my b1, b2, etc variables to the variable row1, etc. in the RandNum class. I know that I could just put it all in one class but this is a problem I have had multiple times so I'd like to solve it. Any help is much appreciated.

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132

4 Answers4

1

Your b etc. variables are scoped to the main method of Bingo so you'll never be able to access them from another class - or another method of the same class for that matter.

You could set them as public static fields of Bingo instead of declaring them within the method's body (or privates with getters), but due to the number of variables you have here, I suggest refactoring first and investigating a few data structures such as Collections and Maps.

Mena
  • 47,782
  • 11
  • 87
  • 106
1

What happens is that you are declaring rowX variables as local variables in your randNum() method.

This makes that its scope is only that method, so they won't exist / be available outside it.

What can you do? Declare them as class attributes:

public class RandNum {
    int row1, row2, row3, row4, row5;
    ...
    public void randNum() {...}
}

Note: You could use an array instead:

int[] rows = new int[5];

Then in your method:

public void randNum() {
    Random rn = new Random();

    for (int i = 0; i < rows.length(); i++)
        rows[i] = rn.nextInt(max - min + 1) + min;
}
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
0

This should solve it. The variables b1,b2,... are local to your randNum function and will be out of scope once execution exits the function. The solution is to store the variables in a field in the RandNum class.

public class RandNum{
    private int[] rows = new int[5];
    private int min;
    private int max;

    public RandNum(int min, int max) {
        Random rn = new Random();
        for(int i=0;i<5;++i)
           rows[i] = rn.nextInt(max - min + 1) + min;
    }

    public int getRow(int row) {
       return rows[i];
    }
}

public static void main (String[] args) {
    RandNum columnB = new RandNum(1,15);
    int b1 = columnB.getRow(0);
    int b2 = columnB.getRow(1);
    int b3 = columnB.getRow(2);
    int b4 = columnB.getRow(3);
    int b5 = columnB.getRow(4);

    ...
}
Niels Billen
  • 2,189
  • 11
  • 12
0

See: Why use getters and setters?

Create accessors/mutators:

public class RandNum {
    private int min;
    private int max;

    public RandNum(int min, int max) {
        setMin(min);
        setMax(max);
    }

    public RandNum() {
        this(1, 0);
    }

    public int getMin() {
        return min;
    }

    public int getMax() {
        return max;
    }

    public void setMin(int min) {
        this.min = min;
    }

    public void setMax(int max) {
        this.max = max;
    }

    // ...
}

Then use them:

public class Bingo {
    public static void main (String[] args) {
        RandNum columnB = new RandNum();
        columnB.setMax(15);
        // ...
    }
}
Community
  • 1
  • 1
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132