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.io.*;

public class FileReader
{
   public static void main(String args[]) throws Exception
   { 

   FileReader fr = new FileReader("animal.txt"); 
   BufferedReader br = new BufferedReader(fr); 
   String s; 
   while((s = br.readLine()) != null)
   { 
      System.out.println(s); 
   } 
   fr.close(); 
   } 
}

The following code segment is animal.txt

3000,Monkey,38.6
7999,Ape,65.2,
8000,Dog,13.4,Thomas,George,
5252,Giraffe,130.3,103,Samuel,

The problem I'm having so far is that my program prints:

3000,Monkey,38.6
7999,Ape,65.2,
8000,Dog,13.4,Thomas,George,
5252,Giraffe,130.3,103,Samuel,

Rather than what I want it to print:

ID: 3000
Type: Monkey
Weight: 38.6

ID: 7999
Type: Ape
Weight: 65.2

(The above from the animal class)

ID: 8000
Type: Dog
Weight: 13.4
Name: Thomas
Owner: George

(The above from the pet class)

ID: 5252
Type: Giraffe
Weight: 130.3
Cage Number: 103
Trainer: Samuel

(The above from the zoo animal class)

What can I do so my program prints the desired output?

You need to make an animal. You are just reading from the text file and printing the information to the screen.

The class FileReader could look like this
Here I renamed it to MyFileReader to avoid the confusion with the calss FileReader that is in the package java.io

public class MyFileReader {

    public static void main(String args[]) throws Exception {

        FileReader fr = new FileReader("animal.txt");
        BufferedReader br = new BufferedReader(fr);
        String s;
        while ((s = br.readLine()) != null) {
            Animal animal = null;
            //System.out.println(s);
            String[] info = s.split(",");
            int animalID = Integer.parseInt(info[0]);
            String animalType = info[1];
            double weight = Double.parseDouble(info[2]);
            int cageNumber;
            String name;
            String trainer;
            String owner;
            if (info.length == 5) {
                try {
                    //ZooAnimal
                    cageNumber = Integer.parseInt(info[3]);
                    trainer = info[4];
                    animal = new ZooAnimal(animalID, animalType, weight,cageNumber,trainer);
                    System.out.println(((ZooAnimal)animal).toString());
                } catch (Exception e) {
                    //Pet
                    name = info[3];
                    owner = info[4];
                    animal = new Pet(animalID, animalType, weight,name,owner);
                    System.out.println(((Pet)animal).toString());
                }

            } else {
                animal = new Animal(animalID, animalType, weight);
                System.out.println(animal.toString());
            }

        }
        fr.close();
    }
}

it gives the right out put;
sure other solutions are possible

Hope it helps.

You must take each line the way you do now and extract the data:
> 3000,Monkey,38.6

3000
Monkey
38.6

Then with that data create an Animal instance and print it.
For that there is the split method:

String s = "3000,Monkey,38.6";
String [] tokens = s.split(",");
// tokens array now has: {"3000", "Monkey", "38.6"};
tokens[0] : Id
tokens[1] : Type
tokens[2] : Weight
// its length is tokens.length = 3

So if the line is:
> 5252,Giraffe,130.3,103,Samuel,
The length of the array would be 5.

Use that to determine what kind of object to create an Animal, or a Pet.

@javaAddict please take alook to the code I made.
Is the code I wrote wrong?

The class FileReader could look like this
Here I renamed it to MyFileReader to avoid the confusion with the calss FileReader that is in the package java.io

public class MyFileReader {

    public static void main(String args[]) throws Exception {

        FileReader fr = new FileReader("animal.txt");
        BufferedReader br = new BufferedReader(fr);
        String s;
        while ((s = br.readLine()) != null) {
            Animal animal = null;
            //System.out.println(s);
            String[] info = s.split(",");
            int animalID = Integer.parseInt(info[0]);
            String animalType = info[1];
            double weight = Double.parseDouble(info[2]);
            int cageNumber;
            String name;
            String trainer;
            String owner;
            if (info.length == 5) {
                try {
                    //ZooAnimal
                    cageNumber = Integer.parseInt(info[3]);
                    trainer = info[4];
                    animal = new ZooAnimal(animalID, animalType, weight,cageNumber,trainer);
                    System.out.println(((ZooAnimal)animal).toString());
                } catch (Exception e) {
                    //Pet
                    name = info[3];
                    owner = info[4];
                    animal = new Pet(animalID, animalType, weight,name,owner);
                    System.out.println(((Pet)animal).toString());
                }

            } else {
                animal = new Animal(animalID, animalType, weight);
                System.out.println(animal.toString());
            }

        }
        fr.close();
    }
}

it gives the right out put;
sure other solutions are possible

Hope it helps.

Try not to give away ready solution. And if you do try to explain what you did.
In this post you didn't explain anything. You expect the reader to understand your code and if they don't, he has code that works but does not know what it does and why.
That could be more confusing because in the future when they try to do something similar they wouldn't know how to work around that code.

But it seem that the OP can read some simple code like the above one; he wrote a good structure of polymorphisme and I suppose he can read it especialy it is not so complicated.
But I'm aggred every code must be well documented sure.
Thinks.

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.