zxcvbnm
zxcvbnm

Reputation: 1

Adding a variable element to an array in java

  String plants [] = {"Sunflower", "Pea Shooter", "Cherry Bomb", "Wall-Nut", "Potato Mine", "Snow Pea", "Chomper    "};

  int sunlight [] = {50, 100, 150, 50, 25, 175, 150};

  for (int i = 0; i < plants.length; i++) {
   System.out.println ((i+1) + "\t" + plants [i] + "\t" + sunlight [i]);
   }

This prints out the array regularly, and this part works.

    String addplant = IBIO.inputString ("\nWhich plant do you add? ");
    int addsl = IBIO.inputInt ("How much sunlight do you add to that plant? ");

    plants [] = {"Sunflower", "Pea Shooter", "Cherry Bomb", "Wall-Nut", "Potato Mine", "Snow Pea", "Chomper    ", addplant};

    sunlight [] = {50, 100, 150, 50, 25, 175, 150, addsl};

    for (int i = 0; i < plants.length; i++) {
    System.out.println ((i+1) + "\t" + plants [i] + "\t" + sunlight [i]); }

Similarly, I want a new array to be printed out based on the value the user inputs. The new element should appear at the end of the array.

I must get input using IBIO.input. It stores the user input into a variable.

However, what I tried does not work. Instead, it just says "VariableDeclaratorId expected after this token" and highlights the new array I made.

I am wondering how I would add a variable element to my array, and am unsure of how to do this. I tried googling many things but nothing seemed to have worked.

Upvotes: 0

Views: 206

Answers (3)

Shadow
Shadow

Reputation: 4016

Arrays cannot be added to. You probably know this since you are reassigning the value of the array, putting in all the old elements plus the new one. This will cause problems because you've hard-coded it and the next time an item is added it will not contain any previously added new elements.

The best thing to do is to use a map, to store the plant and the amount of sunlight, like this:

Map<String, Integer> plantsAndSunlight = new HashMap<String, Integer>();
plantsAndSunlight.put("Sunflower", 50);
plantsAndSunlight.put("Peashooter", 100);
:
:

Then to add a new elements just add it to the end using the put function from above, passing in the elements you got from user inout:

String addplant = IBIO.inputString ("\nWhich plant do you add? ");
int addsl = IBIO.inputInt ("How much sunlight do you add to that plant? ");

plantsAndSunlight.put(addplant, addsl);

Upvotes: 1

netsplit
netsplit

Reputation: 1130

Arrays have a constant size, you can't resize them. Probably what you want is an ArrayList. Also you didn't ask but I noticed you have two arrays of related data correlated by index, this can get messy. Since plants are likely to have more than two kinds attributes I suggest using a class like this:

   private class Plant {
    String plant;
    int sunlight;

    public Plant(String plant, int sunlight) {
        this.plant = plant;
        this.sunlight = sunlight;
    }
}


public void someMethod() {
    ArrayList<Plant> plants = new ArrayList<Plant>();
    plants.add( new Plant("Sunflower", 50));
    plants.add( new Plant("Pea Shooter", 100));
    ...


    for (int i = 0; i < plants.size(); i++) {
        System.out.println((i + 1) + "\t" + plants.get(i).plant + "\t" + plants.get(i).sunlight);
    }
}

Then if you delete Pea Shooters, you won't have the chance to forget deleting the sunlight element for Pea Shooters. Plus it's nice and clean if you decide to something like this:

   private class Plant {
    String plant;
    int sunlight;
    float soilPh;

    public Plant(String plant, int sunlight, float soilPh) {
        this.plant = plant;
        this.sunlight = sunlight;
        this.soilPh = soilPh;
    }
}


public void someMethod() {
    ArrayList<Plant> plants = new ArrayList<Plant>();
    plants.add( new Plant("Sunflower", 50, 6.5f));
    plants.add( new Plant("Pea Shooter", 100, 7f));
    ...


    for (int i = 0; i < plants.size(); i++) {
        System.out.println((i + 1) + "\t" + plants.get(i).plant + "\t" + plants.get(i).sunlight + "\t" + plants.get(i).soilPh);
    }
}

Edit you need to import:

import java.util.ArrayList;

Upvotes: 3

Nikolas
Nikolas

Reputation: 44496

The better and the more "Java" way is to use List, because it has a better structure for manipulation of its items.

ArrayList<String> plants = new ArrayList<>(Arrays.asList("Sunflower", "Pea Shooter", "Cherry Bomb", "Wall-Nut", "Potato Mine", "Snow Pea", "Chomper"));
ArrayList<Integer> sunlight = new ArrayList<>(Arrays.asList(50, 100, 150, 50, 25, 175, 150));

Then you just add an item to the List with the method add() to the end.

plants.add(addplant);
sunlight.add(addsl);

Getting the wanted item works similar like with arrays. Use the get(index) method. The following method assume your lists of the same size.

for (int i=0; i<plants.size(); i++) {
    System.out.println((i+1) + "\t" + plants.get(i) + "\t" + sunlight.get(i)); 
}

In the other case you would change the condition in your for-cycle to Math.min(plants.size(), sunlight.size()).


Also the usage of Map would be a good idea.

Map<String, Integer> myMap = new HashMap<>();

Adding items with the method put(...).

Upvotes: 1

Related Questions