Results 1 to 2 of 2
  1. #1
    javanewbie82 is offline Member
    Join Date
    Oct 2016
    Posts
    13
    Rep Power
    0

    Default How do I determine values outside of my fileStream?

    I have an assignment where I need to order pet records in various ways. My issue is that I need to determine the values and print them outside of the fileStream. I think that I have the file reading part of the problem correct but I have no idea how to use the values once I have closed the program.
    My program:
    import java.io.FileInputStream; // replaces the FileInputStream class
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    import java.io.IOException;
    import java.util.StringTokenizer;

    Java Code:
    public class PettArray
    {
                     // my files name
    	private static final String FILE_NAME = "pets.txt.rtx";
    	public static void main (String[] args) 
    	{         
                        // empty string to read the file
    		String line = "";
    		double heaviest = 0; 
    		int oldest = 0;
    	              // initialize FileInputStream
    		FileInputStream fileInputStream = null ;
                         // open the file and read it
    		try
     {
        fileInputStream = new FileInputStream(FILE_NAME) ;
     }
     catch (FileNotFoundException e)
     {
        System.out.println("Couldn't find file " + FILE_NAME + " - aborting!") ;
        System.exit(0) ;
     }
     Scanner petScanner = new Scanner(fileInputStream) ;
    	PetRecord[] pets =new PetRecord [10];
    		while (petScanner.nextLine() != null)
    		{
    			for (int i = 0; i < pets.length; i++)
    			{
    				
    				line = petScanner.nextLine();
    				StringTokenizer tokenizer = new StringTokenizer (line, ",");
    				String name = tokenizer.nextToken();
    				int age = Integer.parseInt(tokenizer.nextToken());
    				double weight = Double.parseDouble(tokenizer.nextToken());
    				pets [i] = new PetRecord (name, age, weight);
    		}
    		try 
    		{
    			fileInputStream.close();
    		}
    		catch (IOException e)
    		{
    			System.out.println ("Unable to close file");
    		}
    			/* from here I am struggling as it won't recognize i
    				if (pets[i].getWeight() < heaviest)
    				{
    					heaviest = pets[i].getWeight();
    					System.out.println("The heaviest pet is:" + pets[i].getName() + ", and weighs" + heaviest + ", and is " + pets[i].getAge + " years old");
    				}
    			
    			
    	}
    }
    }
    
    // PetRecord Class:
    public class PetRecord
    {
        // Instance variables
        private String name;
        private int age;             //in years
        private double weight;       //in pounds
    
        // Default values for instance variables
        private static final String DEFAULT_NAME = "No name yet." ;
        private static final int DEFAULT_AGE = -1 ;
        private static final double DEFAULT_WEIGHT = -1.0 ;
    
       /***************************************************
        * Constructors to create objects of type PetRecord
        ***************************************************/
    
        // no-argument constructor
        public PetRecord( )
        {
            this(DEFAULT_NAME, DEFAULT_AGE, DEFAULT_WEIGHT) ;
        }
        // only name provided
        public PetRecord(String initialName)
        {
            this(initialName, DEFAULT_AGE, DEFAULT_WEIGHT) ;
        }
        // only age provided
        public PetRecord(int initialAge)
        {
            this(DEFAULT_NAME, initialAge, DEFAULT_WEIGHT) ;
        }
        // only weight provided
        public PetRecord(double initialWeight)
        {
            this(DEFAULT_NAME, DEFAULT_AGE, initialWeight) ;
        }
        // full constructor (all three instance variables provided)
        public PetRecord(String initialName, int initialAge, double initialWeight)
        {
            setName(initialName) ;
            setAge(initialAge) ;
            setWeight(initialWeight) ;
        }
    
       /****************************************************************
        * Mutators and setters to update the Pet.  Setters for age and
        * weight validate reasonable weights are specified
        ****************************************************************/
    
        // Mutator that sets all instance variables
        public void set(String newName, int newAge, double newWeight)
        {
            setName(newName) ;
            setAge(newAge) ;
            setWeight(newWeight) ;
        }
    
        // Setters for each instance variable (validate age and weight)
        public void setName(String newName)
        {
            name = newName;
        }
        public void setAge(int newAge)
        {
            if ((newAge < 0) && (newAge != DEFAULT_AGE))
            {
                System.out.println("Error: Invalid age.");
                System.exit(99);
            }
            age = newAge;
        }
        public void setWeight(double newWeight)
        {
            if ((newWeight < 0.0) && (newWeight != DEFAULT_WEIGHT))
            {
                System.out.println("Error: Invalid weight.");
                System.exit(98);
            }
            weight = newWeight;
        }
    
       /************************************
        * getters for name, age, and weight
        ************************************/
        public String getName( )
        {
            return name;
        }
        public int getAge( )
        {
            return age;
        }
        public double getWeight( )
        {
            return weight;
        }
    
       /****************************************************
        * toString() shows  the pet's name, age, and weight
        * equals() compares all three instance variables
        ****************************************************/
        public String toString( )
        {
            return ("Name: " + name + "  Age: " + age + " years"
                           + "   Weight: " + weight + " pounds");
        }
        public boolean equals(PetRecord anotherPetRecord)
        {
            if (anotherPetRecord == null)
            {
                return false ;
            }
            return ((this.getName().equals(anotherPetRecord.getName())) &&
                    (this.getAge() == anotherPetRecord.getAge()) &&
                    (this.getWeight() == anotherPetRecord.getWeight())) ;
        }
    }

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Re: How do I determine values outside of my fileStream?

    You should make this a two-stage rocket: step 1 is to read all the Pet records, step 2 is to do something with the array. You have nailed step one apparently. Now create step 2: Start a new loop over the array and do your checks there.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Replies: 6
    Last Post: 05-29-2012, 01:47 PM
  2. Replies: 1
    Last Post: 02-26-2012, 08:10 AM
  3. How to determine CPU usage using Java?????
    By JavaEmpires in forum New To Java
    Replies: 5
    Last Post: 03-16-2011, 05:49 AM
  4. how to determine the primary key
    By osval in forum JDBC
    Replies: 1
    Last Post: 08-07-2007, 03:31 AM
  5. Replies: 1
    Last Post: 05-20-2007, 12:19 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •