2

I am pretty new to Java and had a question. This is homework, so I would not like any outright answers. Thanks!

I'm working on a genetic algorithm for playing poker. I wanted to create an arrayList of string arrays. The string arrays would hold moves for each possible hand during a round. What I want to do right now to make sure it is working is run my method and print out the results. Here are my practice classes (this is only a small part of the assignment):::

import java.util.ArrayList;
import java.util.Arrays;


public class Play 
{

    GeneticAlg start;
    ArrayList<Object> pop;

    public static void main(String[] args) 
    {
        GeneticAlg start = new GeneticAlg();
        ArrayList<String[]> pop = start.initializePopulation();

        for(String[] arr: pop)
        {
            System.out.println(Arrays.toString(arr));
        }

    }

}

import java.util.ArrayList;
import java.util.Random;


public class GeneticAlg 
{
    ArrayList<String[]> population;
    int[] populationScores;
    String[] chromosome;
    int generation;
    int index;

    public GeneticAlg()
    {

    }

    public ArrayList<String[]> initializePopulation()
    {
        ArrayList<String[]> population = new ArrayList<String[]>();

        for (int i = 0; i < 20; i++)
        {
            Random generator = new Random();
            String[] choices = {"bet","raise","call","check"};
            chromosome = new String[33];

            for (int j = 0; j < 33; j++)
            {
                if (j < 6) //first and second round possible hands)
                {
                    index = generator.nextInt((choices.length)-1); 
                    chromosome[j] += choices[index];
                }

                else //third, fourth, and fifth round possible hands)
                {   
                    index = generator.nextInt(choices.length);
                    chromosome[j] += choices[index];
                }
            }

            population.add(chromosome); 
        }

        return population;
    }

}

Right now, it's printing out the array, but each entry looks like this:

[nullcall, nullraise, nullbet, nullraise, nullcall, nullraise,....

I want it to just return the move without null on the front. Any advice is appreciated. Thanks!

ETA: I fixed the two lines with the concatenation error, but it is still printing "null" in front of each command. Any advice?

Code after repairing the error:

import java.util.ArrayList;
import java.util.Arrays;


public class Play 
{

    GeneticAlg start;
    ArrayList<Object> pop;

    public static void main(String[] args) 
    {
        GeneticAlg start = new GeneticAlg();
        ArrayList<String[]> pop = start.initializePopulation();

        for(String[] arr: pop)
        {
            System.out.println(Arrays.toString(arr));
        }

    }

}

import java.util.ArrayList;
import java.util.Random;


public class GeneticAlg 
{
    ArrayList<Object> population;
    String[] choices;
    int[] populationScores;
    String[] chromosome;
    int generation;
    int index;

    public GeneticAlg()
    {

    }

    public ArrayList<Object> initializePopulation()
    {
        population = new ArrayList<Object>();

        for (int i = 0; i < 20; i++)
        {
            Random generator = new Random();

            for (int j = 0; j < 24; j++)
            {
                if (j < 6) //first and second round possible hands)
                {
                    choices[0]= "bet";
                    choices[1]= "raise";
                    choices[3]= "call";

                    index = generator.nextInt(choices.length); 
                    chromosome[j] = choices[index];
                }

                else //third, fourth, and fifth round possible hands)
                {
                    choices[4] = "check";

                    index = generator.nextInt(choices.length);
                    chromosome[j] = choices[index];
                }
            }

            population.add(chromosome); 
        }

        return population;
    }

}

4 Answers 4

4

Arrays of objects (like Stirngs) are filled at start with null values, so doing

chromosome[j] += choices[index];

is the same as

chromosome[j]  = chromosome[j] + choices[index];

which is the same as

chromosome[j]  = null + choices[index];

So you are concatenating null with choices[index]; which gives you nullbet for instance.

To solve it just use = instead of +=.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! I fixed the two lines that contained this error, but it is still giving me "nullbet" instead of "bet." Any ideas?
That is unusual. I don't see any nulls after fixing two lines with this problem. Did you add anything new to your code?
Code you posted doesn't compile because of <Object> and <String[]> incompatibility. When I corrected it it seem it throws NPE when you try to assign bet to choices[0] = "bet"; because you didn't initialized array yet.
Post code which we could use to actually reproduce your problem, because now I am just guessing that you didn't initialize rest of your choices arrays and they are still nulls there (your code sets up only indexes [0] [1] [3] and [4], while [5] [6] may be not changed so they will contain null values.
2

Since you don't want an outright answer, look at the code that populates your String[]'s. Your printing's doing the right thing, but the Strings in the array actually are "nullbet," "nullraise," etc.

1 Comment

Thank you! This helped me make more sense of it.
1

The error comes from this line here:

chromosome[j] += choices[index];

The += operator, when used with a Strings, will concatenate the right-hand string to the end of the left-hand string. In this case, it tacks on choices[index] to the existing contents of chromosome[j], which will null by default if chromosome is declared as an array of Objects.

You probably meant

chromosome[j] = choices[index];

And accidentally inserted the + because you use population.add() with your list below.

2 Comments

Dang it, Pshemo, that was a quick response. I guess I need to type faster. ;)
Thanks! I fixed the two lines that contained this error, but it is still giving me "nullbet" instead of "bet." Any ideas?
1

Your "fixed" code looks like it doesn't compile - you're probably still running the previous version. You don't initialize choices ("choices = new String[4]") and you're confusing its indices (0, 1, 3 and 4). Also, if you're trying to add a fourth element to the array later, don't, you can't do that with arrays. And you're assigning an ArrayList to an ArrayList without a cast. You only needed to swap += with = in your original code, it seemed fine otherwise.

1 Comment

Thanks. I was running the previous version on accident. It's fixed now!

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.