简体   繁体   中英

Java - how to call an add() method in a different class

This is for homework, and I am becoming a little frustrated with how I can't figure out something so simple.

To simplify my code, I have 3 files right now: one class with an add() method I created among other things, one file that tests it (made by the prof), and one that creates the object (which I won't post, b/c its working). Here's the add() function.

EDIT 2: I'm going to add the method that prints the array, maybe that's the problem?

    public class Population {
      private Person[] pop = new Person[15];
      private int numPop = 0;    

      public void add(Person c){ // this object is created in another class, it works fine
        for(int i = 0; i < pop.length; i++){
          if(pop[i] == null) {
            pop[i] = c;
            numPop++;
          } else {}
        }

 public String listPeople(){
      System.out.println("Population with "+numPeople+" people as follows:");
      int i = 0;
      while (i<numPeople){
       System.out.println("A "+pop[i].getAge()+"year old person named "+pop[i].getName());
        i++;
//FYI the get methods are working fine and are in another file.
 } 
      return("");
      }

Then, I run the program in a test file to ensure it works, which was provided to us. Here's the part that isn't working

public class PopTestProgram{ // FYI the prof created this, I can't change this
  public static void main(String[] args){

    Population pop = new Population(15);

    pop.add(new Person(4, "Bob"));
    pop.add(new Person(25, "Kim"));
    // then adds 8 more people with different ages and names
    // then prints the people

It compiles, but when I run it, it just puts 10 of the last person into the array, then crashes saying there is a problem with the "pop[i] = c;" line. I simply cannot figure out what I need to change here.

I haven't received an email from the prof directly, so I thought I'd ask here.

Edit: Here's what it shows after printing out the last person 10 times. It is showing problems with other methods that I haven't completed yet though...

java.lang.ArrayIndexOutOfBoundsException: -1
    at Population.removePerson(Population.java:49)
    at PopTestProgram.main(PopTestProgram.java:31)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

In add(Person), you are not stopping when you add an item, so the first item you added is put in all cells in the array, then the rest won't go in at all, since there are no more null cells in array. Break the loop when you find an empty location.

public void add(Person c) {
    for(int i = 0; i < pop.length; i++){
      if(pop[i] == null) {
        pop[i] = c;
        numPop++;
        break;
      }
      else {
        //.....
      }
   }
}

Could also just use numPop as the next location in the list, like:

public void add(Person c) {
    if (numPop < pop.length) {
       pop[numPop++] = c;
    }
}

The exception is coming in at Population.removePerson(Population.java:49) , which is not related to add method. So I assume removePerson is the method to print the person. While removing you are calling one extra For loop , make sure your iteration is 10 times only.

The java.lang.ArrayIndexOutOfBoundsException: -1 clearly tells the removePerson method is calling the index -1 also (which doesnt exist causing ArrayIndexOufofBoundsException). The removePerson method should start from index 9 to index 0 (or viceversa) [10 iteration in total] and then stop out.

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM