Вопрос

I am creating a program that simulates some people catching fish in a lake, I already created classes for Fish and Pond and I was working on the Fisher class and a method is not working and I'll show the code (I'm new to programming so I'm not sure if I am providing enough information)

public class Fisher {

  public static int LIMIT = 3;
  private String     name;
  private Fish[]     fishCaught = new Fish[LIMIT];
  private int        numFishCaught;
  private int        keepSize;

  public Fisher(String name, Fish[] fishCaught, int numFishCaught, int keepSize) {

    this.name = name;
    this.fishCaught = fishCaught;
    this.numFishCaught = numFishCaught;
    this.keepSize = keepSize;
  }

  public String getName() {
    return name;
  }

  public Fish[] getFishCaught(){
    return fishCaught;
  }

  public int getNumFishCaught() {
    return numFishCaught;

  }

  public int getKeepSize() {
    return keepSize;
  }

  public String toString() {
    return (name + " with " + numFishCaught + " fish");
  }

  public void keep(Fish f) {
    if (numFishCaught == LIMIT) {
    } else {
      numFishCaught++;
      fishCaught[numFishCaught-1] = f;
    }

  }

  boolean likes(Fish f) {
    if ((f.getSize() >= keepSize) && !(f.getSpecies().equalsIgnoreCase("Sunfish"))) {
      return true;
    }
     return false; 
  }

  public void listFish(){
    System.out.println(name + " with " + numFishCaught + " as follows: ");
    for (int i = 0; i<numFishCaught; i++){
      Fish f = new fish[i];
      System.out.println("A " + f.getSize() + " cm " + f.getFishCaught());
    }
  }

}

the problem is the listFish() method, it's supposed to return something like this:

Bob with 2 fish as follows:
A 4 cm Pike
A 15 cm Bass

but it's not working it gives me "incompatible types" and "cannot find symbol" errors??

(just to make your life easier i'll include the Fish class too)

public class Fish {

  private String      species;
  private int         size;


  public Fish(int size, String species) {
    this.size = size;
    this.species = species;
  }


   public String toString() {
          return " A " + size + " cm " + species;
    }

  public String getSpecies() {
    return species;
  }

  public int getSize() {
    return size;
  }


}


Error: /Users/halahalhomoud/Fisher.java:57: incompatible types
found   : Fish[]
required: Fish
File: /Users/halahalhomoud/Fisher.java  [line: 58]
Error: /Users/halahalhomoud/Fisher.java:58: cannot find symbol

EDIT: show you how? I used it in the Pond class and it worked fine but I don't get why it's not working here.

Это было полезно?

Решение

You want the fish that are caught by a Fisher. Now, you have that information in the array you can retrieve with getFishCaught.

Now look what you try to do instead:

 Fish f = new fish[i];
 System.out.println("A " + f.getSize() + " cm " + f.getFishCaught());

In the first line, you try to make a new array of fish, but it is, of course Fish (fish is the symbol that couldn't get resolved.). Then you try to assign the array reference to a single Fish f. But an array of Fish is not the same as a Fish. For example, you can eat a Fish, but not a Fish container, you know.

What you probably want is this:

 Fish f = (getFishCaught())[i];   // get the i-th Fish caught
 System.out.println("A " + f.getSize() + " cm " + f.getXXX());

where getXXX is a method of Fish that returns the Fishs species. (Since you didn't show the FIsh class, I can't know the exact name of this getter).

Другие советы

Fish f = new fish[i];

should be:

Fish f = fishCaught[i];

Complete Method

  public void listFish(){
    System.out.println(name + " with " + numFishCaught + " as follows: ");
    for (int i = 0; i<numFishCaught; i++){
      if(fishCaught[i] != null){
          Fish f = fishCaught[i];
          System.out.println("A " + f.getSize() + " cm " + f.getSpecies());
      }
    }
  }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top