Distinguishing integer numbers from if statements?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • monkey0525
    New Member
    • Apr 2010
    • 11

    #1

    Distinguishing integer numbers from if statements?

    I'm writing a program that reads information from three seperate classes. Here is my code:
    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;  
       }
    }
    Code:
    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;  
       }
    }
    Code:
    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;
       }
    }
    Code:
    import java.io.*;
    
    public class FileRead
    {
       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);
             Integer[] info = s.split(",");
             int animalID;
             String animalType;
             double weight;
             int cageNumber;
             String name;
             String trainer;
             String owner;
             if (info <= 8000 || info > 9999)
    	 {
                animal = new ZooAnimal(animalID, animalType, weight,cageNumber,trainer);
                System.out.println(((ZooAnimal)animal).toString());
             }
    
             else 
             {
    	    if (info < 1000 || info > 9999)
    	    System.out.println("The ID number you entered is invalid.");
    	 }
                if (info < 3000 || info > 7999)
    	    {
                   //Pet
                   animal = new Pet(animalID, animalType, weight,name,owner);
                   System.out.println(((Pet)animal).toString());
                }		
    	    else 
    	    {
    	       if (info < 1000 || info > 9999)
    	       System.out.println("The ID number you entered is invalid.");
    	    }
    	
    	       if (info < 1000 || info > 2999)
    	       {
                      animal = new Animal(animalID, animalType, weight);
                      System.out.println(animal.toString());
                   }
    					
    	       else 
    	       {
    	          if (info < 1000 || info > 9999)
    		  System.out.println("The ID number you entered is invalid.");
    	       }		
          }
          fr.close();
       }
    }
    The following code segment is animal.txt

    Code:
    3000,Monkey,38.6
    7999,Ape,65.2,
    8000,Dog,13.4,Thomas,George,
    5252,Giraffe,130.3,103,Samuel,
    I want my program to be able to distinguish the different ID numbers from the above classes, so it knows which ID number is an animal, pet or zoo animal. The ID number between 1000 and 2999 is an animal, the ID number between 3000 and 7999 is a pet and an ID number between 8000 and 9999 us a zoo animal. Any ID number which is less than 1000 or greater than 9999 should be considered invalid records.

    Here is my desired output:

    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)

    The error that's throwing me off so far is that my if statements aren't processing through due to the fact that:

    FileRead.java:2 2: operator <= cannot be applied to java.lang.Integ er[],int

    What am I doing wrong that's preventing me from my desired output?
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    info is an array. You can not apply logical operators to an array. but you can apply it to elements of array.

    BTW, you are declaring an array of Integer objects not primitive data type

    Code:
    Integer[] a;
    int[] b
    Please note the difference between these two.

    Regards
    Dheeraj Joshi

    Comment

    Working...