473,416 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Distinguishing integer numbers from if statements?

I'm writing a program that reads information from three seperate classes. Here is my code:
Expand|Select|Wrap|Line Numbers
  1. public class Animal
  2. {
  3.    protected int id;
  4.    protected String type;
  5.    protected double mass;
  6.  
  7.     //------------------------------------------------------------------------
  8.     //  Sets up an animal with the specified ID number, type and weight.
  9.     //------------------------------------------------------------------------
  10.  
  11.    public Animal (int animalID, String animalType, double weight)
  12.    {
  13.       id = animalID;
  14.       type = animalType;
  15.       mass = weight;
  16.    }
  17.  
  18.    //------------------------------------------------------------------------
  19.    //  Returns information about this animal as a string.
  20.    //------------------------------------------------------------------------
  21.  
  22.    public String toString()
  23.    {
  24.       String description = "ID: " + id + "\n";
  25.  
  26.       description += "Type: " + type + "\n";
  27.       description += "Weight: " + mass + "\n"; 
  28.  
  29.       return description;  
  30.    }
  31. }
  32.  
Expand|Select|Wrap|Line Numbers
  1. public class Pet extends Animal
  2. {
  3.    private String title;
  4.    private String own;
  5.  
  6.    //------------------------------------------------------------------------
  7.    //  Sets up a pet using the specified information.
  8.    //------------------------------------------------------------------------
  9.  
  10.    public Pet (int animalID, String animalType, double weight, String name, String owner)
  11.    {
  12.       super (animalID, animalType, weight);
  13.  
  14.       title = name;
  15.       own = owner;
  16.    }
  17.  
  18.    //------------------------------------------------------------------------
  19.    //  Returns information about this pet as a string.
  20.    //------------------------------------------------------------------------
  21.  
  22.    public String toString()
  23.    {
  24.       String description = super.toString();
  25.  
  26.       description += "Name: " + title + "\n";
  27.       description += "Owner: " + own + "\n"; 
  28.  
  29.       return description;  
  30.    }
  31. }
  32.  
Expand|Select|Wrap|Line Numbers
  1. public class ZooAnimal extends Animal
  2. {
  3.    private int cage;
  4.    private String train;
  5.  
  6.    //------------------------------------------------------------------------
  7.    //  Sets up a zoo animal using the specified information.
  8.    //------------------------------------------------------------------------
  9.  
  10.    public ZooAnimal (int animalID, String animalType, double weight, int cageNumber, String trainer)
  11.    {
  12.       super (animalID, animalType, weight);
  13.  
  14.       cage = cageNumber;
  15.       train = trainer;
  16.    }
  17.  
  18.    //------------------------------------------------------------------------
  19.    //  Returns information about this zoo animal as a string.
  20.    //------------------------------------------------------------------------
  21.  
  22.    public String toString()
  23.    {
  24.       String description = super.toString();
  25.  
  26.       description += "Cage: " + cage + "\n";
  27.       description += "Trainer: " + train + "\n";
  28.  
  29.       return description;
  30.    }
  31. }
  32.  
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2.  
  3. public class FileRead
  4. {
  5.    public static void main(String args[]) throws Exception
  6.    {
  7.       FileReader fr = new FileReader("animal.txt");
  8.       BufferedReader br = new BufferedReader(fr);
  9.       String s;
  10.       while ((s = br.readLine()) != null)
  11.         {
  12.          Animal animal = null;
  13.             //System.out.println(s);
  14.          Integer[] info = s.split(",");
  15.          int animalID;
  16.          String animalType;
  17.          double weight;
  18.          int cageNumber;
  19.          String name;
  20.          String trainer;
  21.          String owner;
  22.          if (info <= 8000 || info > 9999)
  23.      {
  24.             animal = new ZooAnimal(animalID, animalType, weight,cageNumber,trainer);
  25.             System.out.println(((ZooAnimal)animal).toString());
  26.          }
  27.  
  28.          else 
  29.          {
  30.         if (info < 1000 || info > 9999)
  31.         System.out.println("The ID number you entered is invalid.");
  32.      }
  33.             if (info < 3000 || info > 7999)
  34.         {
  35.                //Pet
  36.                animal = new Pet(animalID, animalType, weight,name,owner);
  37.                System.out.println(((Pet)animal).toString());
  38.             }        
  39.         else 
  40.         {
  41.            if (info < 1000 || info > 9999)
  42.            System.out.println("The ID number you entered is invalid.");
  43.         }
  44.  
  45.            if (info < 1000 || info > 2999)
  46.            {
  47.                   animal = new Animal(animalID, animalType, weight);
  48.                   System.out.println(animal.toString());
  49.                }
  50.  
  51.            else 
  52.            {
  53.               if (info < 1000 || info > 9999)
  54.           System.out.println("The ID number you entered is invalid.");
  55.            }        
  56.       }
  57.       fr.close();
  58.    }
  59. }
  60.  
The following code segment is animal.txt

Expand|Select|Wrap|Line Numbers
  1. 3000,Monkey,38.6
  2. 7999,Ape,65.2,
  3. 8000,Dog,13.4,Thomas,George,
  4. 5252,Giraffe,130.3,103,Samuel,
  5.  
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:22: operator <= cannot be applied to java.lang.Integer[],int

What am I doing wrong that's preventing me from my desired output?
May 6 '10 #1
1 1920
Dheeraj Joshi
1,123 Expert 1GB
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

Expand|Select|Wrap|Line Numbers
  1. Integer[] a;
  2. int[] b
  3.  
Please note the difference between these two.

Regards
Dheeraj Joshi
May 7 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Alpha | last post by:
Hi, I have a window based applicaton. I want to make sure that the users enter integer numbers before I save it to the database. I know to code this in the textbox_validating event but I don't...
45
by: bobalong | last post by:
Hi I'm have some problem understanding how JS numbers are represented internally. Take this code for an example of weirdness: var biggest = Number.MAX_VALUE; var smaller = Number.MAX_VALUE...
8
by: kiranchahar | last post by:
Hey all, How do I generate random numbers with Uniform distribution Uniform(a,b) using C-programming? I want to generate uniform random numbers which have mean following Uniform(p,q) and also...
3
by: dmitrey | last post by:
hi all, I need printing the following: 1 2 3 .... 9 10 ....
4
by: sake | last post by:
Hi, Once again, Google has failed me. I need to know how to parse a string that contains two integer numbers separated by a comma (ex: 6,3). Even though the two numbers are supposed to be single...
14
by: thehobbit | last post by:
Hi, Could anyone give ideas on how to add 4 20 digit numbers in ANSI C and pass the result back to a calling program in COBOL? We were able to add up to 15 digit numbers without any problems,...
5
by: =?Utf-8?B?Y2hyaXNiZW4=?= | last post by:
Hi, I try to use an integer to pass 4 kinds of information. Each kind may have 5 types. I plan to use an integer like 2034, while 2 0 3 4 are corresponding to the type of each kind. I am...
2
by: PythonNotSoGuru | last post by:
Hi all first time posting in a while sry if i mess something up. Anyway I am using 2d arrays to make a game with pygame and i alter coordinates on this array by smaller than 1 increments. Then i need...
1
by: minduser | last post by:
How can I do that? What do I have to do after: (...) while(fscanf(fp,"%d",&c) != EOF) { ? Thanks.
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.