0

So I'm trying to write some code where I basically need to expand an array by one every time I pass through a loop. Yes I know that arrays are fixed size and that I need a new array each time. I'm also aware that if I used a list rather than an array I wouldn't be having any issues however I am being forced to use an array.

My code is so:

  public static void getAnimals()
    {
        String animalName[] = new String[1];
        int animalNumber[] = new int[1];
        int lengthOfAnimals;

        animalName[0] = "Null";
        animalNumber[0] = 0;

        while (!animalName[animalName.length - 1].equals("EXTERMINATE"))
        {


            animalName[animalName.length - 1] = JOptionPane.showInputDialog("Name an animal?");
            String numLeft = JOptionPane.showInputDialog("How many are left?");
            animalNumber[animalNumber.length - 1] = Integer.parseInt(numLeft);

            if (animalName[animalName.length - 1].equals("EXTERMINATE"))
            {
                break;
            }

            else
            {

                System.out.println(animalName[animalName.length - 1]);
                animalName = expandName(animalName);
                animalNumber = expandNumber(animalNumber);

            }               
        }




    }

    public static String[] expandName(String animalName[])
    {

        String animalNameExpanded[] = new String[animalName.length + 1];

        for (int a = 0; a <= animalName.length - 1; a++)
        {
            animalNameExpanded[a] =  animalName[a];
        }


        return animalNameExpanded;
    }

    public static int[] expandNumber(int animalNumber[])
    {
        int animalNumberExpanded[] = new int[animalNumber.length + 1];

        for (int a = 0; a <= animalNumber.length - 1; a++)
        {
            animalNumberExpanded[a] =  animalNumber[a];
        }

        return animalNumberExpanded;
    }    

Now when this runs the value that get puts into animalName and animalNumber is now null. What is going on and how do I fix this.

2
  • 2
    I see no main method. How do you run these methods? To which class do they belong? Commented Oct 15, 2012 at 20:11
  • the main method just calls the getAnimals() method. followed by system exit Commented Oct 15, 2012 at 20:14

2 Answers 2

2
String animalNameExpanded[] = new String[animalName.length + 1];

creates a new array all of whose entries are null. You then copy the values from the old array over, filling all entries but the last one, which remains null.

Add a animalNameExpanded[animalName.length] = "Null"; after the copying, so that

while (!animalName[animalName.length - 1].equals("EXTERMINATE"))

doesn't read a null from the array in the next round.

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

Comments

2

What @Daniel said, but also this what the Arrays.copyOf(T[], int) method is for. Check the docs for how it handles larger/smaller sizes.

Also, really seems like you want an array of Animal objects, rather than separate arrays for different properties.

Edit

Actually, Daniel's suggestion doesn't seem like great style either. Easier to use a null-safe comparison:

while (!"EXTERMINATE".equals(animalName[animalName.length - 1]))

1 Comment

Ditto on the comparison change.

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.