I'm writing a program that reads information from three seperate classes. Here is my code:

public class Animal
{
   protected int id;
   protected String type;
   protected double mass;
 
    //------------------------------------------------------------------------
    //  Sets up an animal with the specified ID number, type and weight.
    //------------------------------------------------------------------------
 
   public Animal (int animalID, String animalType, double weight)
   {
      id = animalID;
      type = animalType;
      mass = weight;
   }
 
   //------------------------------------------------------------------------
   //  Returns information about this animal as a string.
   //------------------------------------------------------------------------
 
   public String toString()
   {
      String description = "ID: " + id + "\n";
 
      description += "Type: " + type + "\n";
      description += "Weight: " + mass + "\n"; 
 
      return description;  
   }
}
public class Pet extends Animal
{
   private String title;
   private String own;
 
   //------------------------------------------------------------------------
   //  Sets up a pet using the specified information.
   //------------------------------------------------------------------------
 
   public Pet (int animalID, String animalType, double weight, String name, String owner)
   {
      super (animalID, animalType, weight);
 
      title = name;
      own = owner;
   }
 
   //------------------------------------------------------------------------
   //  Returns information about this pet as a string.
   //------------------------------------------------------------------------
 
   public String toString()
   {
      String description = super.toString();
 
      description += "Name: " + title + "\n";
      description += "Owner: " + own + "\n"; 
 
      return description;  
   }
}
public class ZooAnimal extends Animal
{
   private int cage;
   private String train;
 
   //------------------------------------------------------------------------
   //  Sets up a zoo animal using the specified information.
   //------------------------------------------------------------------------
 
   public ZooAnimal (int animalID, String animalType, double weight, int cageNumber, String trainer)
   {
      super (animalID, animalType, weight);
 
      cage = cageNumber;
      train = trainer;
   }
 
   //------------------------------------------------------------------------
   //  Returns information about this zoo animal as a string.
   //------------------------------------------------------------------------
 
   public String toString()
   {
      String description = super.toString();
 
      description += "Cage: " + cage + "\n";
      description += "Trainer: " + train + "\n";
 
      return description;
   }
}
import java.util.Scanner;
import java.util.StringTokenizer;
import java.io.*;

public class FileRead
{
   public static void main (String[] args) throws FileNotFoundException
   {
      Scanner scan = new Scanner(new File("animal.txt"));
      String animalType = null, name = null, trainer = null, owner = null, input = null;
      int animalID = 0, cageNumber = 0, animalTotal = 0, petTotal = 0, zooTotal = 0;
      double weight = 0.0;
      StringTokenizer st = null;
      Animal animal = null;
      Pet pet = null;
      ZooAnimal zooAnimal = null;

      System.out.println("\nThis program lists information from a series");
      System.out.println("of animals, pets and zoo animals from a seperate");
      System.out.println("text file. It prints theID number, animal type");
      System.out.println("and weight of each, if the animal is also a pet,");
      System.out.println("in addition. If the animal is a zoo animal, then");
      System.out.println("then a name and owner a cage number and trainer");
      System.out.println("will be added.\n");

      while (scan.hasNextLine())
      {
         input = scan.nextLine();
         st = new StringTokenizer(input, ",");
         animalID = Integer.parseInt(st.nextToken());
	 animalType = st.nextToken();
         weight = Double.parseDouble(st.nextToken());		
			
         if (animalID > 1000 && animalID < 3000)
         {
            animal = new Animal (animalID, animalType, weight);
            System.out.println(animal);
	    animalTotal++;
         }
            if (animalID > 3000 && animalID < 8000)
            {
               name = st.nextToken();
	       owner = st.nextToken();
               pet = new Pet(animalID, animalType, weight, name, owner);
               System.out.println(pet);
	       petTotal++;
            }
	       if (animalID > 8000 && animalID < 10000)
               {
                  cageNumber = Integer.parseInt(st.nextToken());
	          trainer = st.nextToken();
                  zooAnimal = new ZooAnimal(animalID, animalType, weight, cageNumber, trainer);
                  System.out.println(zooAnimal);
	          zooTotal++;
               }
	          if (animalID > 9999 && animalID < 1000)
		  {
		     System.out.println("The ID number is invalid.");
		  }
      }

      System.out.println("\nNumber of animals total: " + animalTotal + ".");
      System.out.println("Number of pets total: " + petTotal + ".");
      System.out.println("Number of zoo animals total: " + zooTotal + ".");
		
      FileWriter fw = new FileWriter("animalout.txt");
      BufferedWriter bw = new BufferedWriter(fw);
      PrintWriter pw = new PrintWriter(bw);

      pw.println(animal.createOutputFile());
      pw.close();
   }
}

The following code segment is animal.txt

1200,Ape,65.2,
5000,Cow,600.8,Bessie,Stu DeFranco,
7999,Dog,13.4,Arf,George Aycoth,
8050,Giraffe,830.3,103,Samuel Trujillo,
9700,Snake,23.0,239,Monica Trevizo,

I'm trying to write the object's data type to the output file, "animalout.txt", but I keep getting an error reading:

FileRead.java:79: cannot find symbol
symbol : method createOutputFile()
location: class Animal
pw.println(animal.createOutputFile());

Recommended Answers

All 2 Replies

The class Animal as posted below dies not provides any createOutputFile function.

Also what will happen if the animalID is 3000? Look at your code. Maybe you need to add some >= to one of the expressions you have in the ifs

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.