Hi. I'm writing a program that reads information from three seperate classes.

public class Animal
{
  protected int id,animalTotal;
  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 String createOutputFile()
   {
      String count = "";

      count += "\nNumber of animals total: " + ProgThree.animalTotal + ".";
      count += "Number of pets total: " + Pet.petTotal + ".";
      count += "Number of zoo animals total: " + ProgThree.zooTotal + ".";

      return count;
   }
}



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 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;  
   }
}



//********************************************************************
//  ZooAnimal.java       
//
//  Program 3
//
//  @Author: Preza
//
//  @Version: 12/01/12
//********************************************************************

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 ProgThree
{
  public static void main (String[] args) throws FileNotFoundException, IOException
  {
   Scanner scan = new Scanner(new File("animal.txt"));
   String animalType = null, name = null, trainer = null, owner = null, lineIn = 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;

   while (scan.hasNextLine())
   {
    lineIn = scan.nextLine();
    st = new StringTokenizer(lineIn, ",");
    animalID = Integer.parseInt(st.nextToken());
    animalType = st.nextToken();
    weight = Double.parseDouble(st.nextToken());            

    if (animalID >= 8000 && animalID <= 9999)
    {
     cageNumber = Integer.parseInt(st.nextToken());
     trainer = st.nextToken();
     zooAnimal = new ZooAnimal(animalID, animalType, weight, cageNumber, trainer);
     System.out.println(zooAnimal);
      zooTotal++;
    }

    if (animalID >= 3000 && animalID <= 7999)
    {
     name = st.nextToken();
     owner = st.nextToken();
     pet = new Pet(animalID, animalType, weight, name, owner);
     System.out.println(pet);
      petTotal++;
    }

    if (animalID >= 1000 && animalID <= 2999)
    {
     animal = new Animal (animalID, animalType, weight);
     System.out.println(animal);
      animalTotal++;
    }

     FileWriter fw = new FileWriter("animalout.txt");
    BufferedWriter bw = new BufferedWriter(fw);
    PrintWriter pw = new PrintWriter(bw);
     pw.println(animal.createOutputFile());
    pw.close();

    }
  }
}



1023,Monkey,38.6
1111,Ape,65.2,
4000,Dog,13.4,Thomas,George,
8100,Giraffe,130.3,103,Samuel,

How do I make this program write an output file that counts how many animals, pets, and zoo animals?

sorry i do not know how to separate these 4 codes.

Recommended Answers

All 8 Replies

you can use the instanceof operator to check of which class each animal is an instance.

You can use a static counter variable in any class to see how many you have created - just increment the counter in the class's constructor, eg

class Demo {
   static int counter = 0;
   public Demo() {
      ... normal constructor code
      counter++
   }

Then you can use that like this:

System.out.println(Demo.counter + " instances of Demo have been created");

I found the problem at line 169 in class ProgThree

int animalID = 0, cageNumber = 0, animalTotal = 0, petTotal = 0, zooTotal = 0;

you are trying to acess animalTotal, petTotal, zooTotal statically from class Animal in method createOutputFile()

public String createOutputFile()
   {
      String count = "";
      count += "\nNumber of animals total: " + ProgThree.animalTotal + ".";
      count += "Number of pets total: " +     ProgThree.petTotal + ".";
      count += "Number of zoo animals total: " + ProgThree.zooTotal + ".";
      return count;
   }

But as per your code animalTotal , petTotal , zooTotal are declared as method local veriables in main method and notdeclared as class variables(static variables).
Hence please modify the class ProgThree by declaring these variables as static variables of the class and remove them from mian method like below

public class ProgThree{
static int animalTotal = 0, petTotal = 0, zooTotal = 0;

  public static void main (String[] args) throws FileNotFoundException, IOException
  {
   Scanner scan = new Scanner(new File("D:\\TR-projects-workspace\\TestResourceBundle\\src\\animal.txt"));
   String animalType = null, name = null, trainer = null, owner = null, lineIn = null;
   int animalID = 0, cageNumber = 0;// animalTotal,  petTotal,  zooTotal have been removed from here 

   /* no changes in rest of the code in this class

   ---
   ----
   ---
   */

Also in Animal class there is a simple mistake below code in method createOutputFile() should be modified

 count += "Number of pets total: " + Pet.petTotal + ".";

 //this line to be modified like below

  count += "Number of pets total: " +ProgThree.petTotal + ".";

Happy coding

Thanks everyone the reply! I took subramanya.vl's suggegstion and everything compiled, but when I run it it says:

Exception in thread "main" java.lang.NullPointerException
    at ProgThree.main(ProgThree.java:67)

This is line 67: pw.println(animal.createOutputFile());

What does this mean?

Thanks in advance

seems animal object is not created

Animal animal = null;// initially animal object is null

For the program to work below lines must be inside the file animal.txt

1023,Monkey,38.6
1111,Ape,65.2,
4000,Dog,13.4,Thomas,George,
8100,Giraffe,130.3,103,Samuel,

Please check contents of file animal.txt.
If this file is blank, then none of the animal objects are created and you will get null pointer exception.

Hi. I have that inside animal.txt.

If I remove this segment then everything runs fine:

     FileWriter fw = new FileWriter("animalout.txt");
    BufferedWriter bw = new BufferedWriter(fw);
    PrintWriter pw = new PrintWriter(bw);

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

but it is not creating an output file.

I think I got it. On animal.java I changed public String createOutputFile() to public static String createOutputFile(). And I changed pw.println(animal.createOutputFile()); to pw.println(Animal.createOutputFile()); on progthree.java. Is this correct?

@jsp418
Is issue resolve?
Please mark this thread to solved/ resolved.

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.