3 Replies - 2838 Views - Last Post: 06 May 2010 - 08:42 AM Rate Topic: -----

#1 velop  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 21-April 09

How to read information from a text file

Posted 06 May 2010 - 06:15 AM

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?

Is This A Good Question/Topic? 0
  • +

Replies To: How to read information from a text file

#2 Dean_Grobler  Icon User is offline

  • D.I.C Regular

Reputation: 39
  • View blog
  • Posts: 390
  • Joined: 15-January 10

Re: How to read information from a text file

Posted 06 May 2010 - 06:56 AM

Maybe this would help.. Reading and writing data from a text file

...Maybe
Was This Post Helpful? 0
  • +
  • -

#3 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: How to read information from a text file

Posted 06 May 2010 - 07:18 AM

you can use the split(String regex) method of String.
check for the API:
http://java.sun.com/...va.lang.String)

it will return an array of String Objects, parsed by the regex you assigned it. *in your case, it should be comma.
then you can just pass the contents of that array as parameters to the appropriate class's constructor, and use the toString method of that Object.
Was This Post Helpful? 0
  • +
  • -

#4 Ember  Icon User is offline

  • D.I.C Head

Reputation: 70
  • View blog
  • Posts: 160
  • Joined: 24-April 10

Re: How to read information from a text file

Posted 06 May 2010 - 08:42 AM

I don't think it has anything to do with his ability to read in files. He has the basis already done for reading it in. From the looks of it, the while loop is where you are going to want to instantiate most of your animals. Here is a little help. I would usually let you do it but the program will be unable to tell the difference between a pet and a ZooAnimal since both will have an array size of 5 and both are nearly indistinguishable. I just used a try-catch block. So if it can't parse the 4th integer, that means it is not a Zoo Animal, so it creates a pet instead:

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; 
//An arrayList is better, if you know how to use one
   ArrayList<Animal> animalArray = new ArrayList<Animal>();
   while((s = br.readLine()) != null)
   { 
     String[] Temp = s.split(",");

     if(Temp.length == 3)
        animalArray.add(new Animal(Integer.parseInt(Temp[0])), Temp[1], Double.parseDouble(Temp[2])));
     else{
        try{
          animalArray.add(new ZooAnimal(Integer.parseInt(Temp[0])), Temp[1], Double.parseDouble(Temp[2]), Integer.parseInt(Temp[3]), Temp[4]));
          
             }
        catch(Exception e){
         
        animalArray.add(new Pet(Integer.parseInt(Temp[0])), Temp[1], Double.parseDouble(Temp[2]), Temp[3], Temp[4]));    
                } 
            }
         }
   fr.close(); 
   } 
}


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1