Results 1 to 2 of 2
- 11-16-2016, 07:35 AM #1
Member
- Join Date
- Oct 2016
- Posts
- 13
- Rep Power
- 0
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())) ; } }
- 11-16-2016, 09:26 AM #2
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
-
Creating a program to determine least amount of coins with values that aren't factors
By Kore in forum New To JavaReplies: 6Last Post: 05-29-2012, 01:47 PM -
Determine if string contains anything but only one letter
By atomant in forum New To JavaReplies: 1Last Post: 02-26-2012, 08:10 AM -
How to determine CPU usage using Java?????
By JavaEmpires in forum New To JavaReplies: 5Last Post: 03-16-2011, 05:49 AM -
how to determine the primary key
By osval in forum JDBCReplies: 1Last Post: 08-07-2007, 03:31 AM -
How to determine if the value of a JTextField is double
By nemo in forum New To JavaReplies: 1Last Post: 05-20-2007, 12:19 PM
Bookmarks