Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 18 of 18

Thread: Printing arrays

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Printing arrays

    I'm trying to print an array of objects.
    Also numOfVehicles seems to be incrementing because when I initialize it to anything above 8, it prints the number plus 8. If numOfVehicles is not initialized of initialized to something less than 9, it gives an ArrayIndexOutOfBoundsException.
    When I'm able to print with out a run-time error, the array just prints null. How do I print the entire array?
    I also have a toString method in the Vehicle class. I have a FleetApp class as well.
    Here's whats in the input files.

    1256BB03457
    Hyundai
    Santa Fe
    2011 18556.0 31195 27301
    true true 5
    5XXCD3AX66
    Kia
    Sorento
    2005 9400.0 67124 60245
    false false 5
    9SEF3AX56M
    FORD
    TAURUS 3.5L
    2001 5400.0 53892 50753
    false true 5
    59867451W
    Honda
    Odyssey Touring Elite
    2011 35000.0 7500 7500
    true true 7

    ADF2462622F
    Chevrolet
    Tahoe
    2007 35000.0 35194 24301
    false 9
    B288413D444
    Ford
    Expedition
    2013 43000.0 5500 3500
    false 8
    GKS61162226
    GMC
    Yukon Hybrid
    2004 51000.0 210000 209000
    true 5
    7865K162563
    Toyota
    Sequoia
    2004 51000.0 210000 209000
    false 8

    public class Fleet
    {
    	Vehicle[] vhcl;
    	private int numOfVehicles;
     
    	public Fleet()
    	{
    		vhcl = new Vehicle[numOfVehicles];
    	}
     
    	public void read(File input1, File input2)
    	{
    		String vin = null, make = null, model = null;
    		int year = 0, i = 0, ctr = 0, passCap;
    		double value = 0, totalMilesDriven = 0, lastOilChange = 0;
    		boolean antiTheft, stability;
    		try
    		{
    			Scanner in1 = new Scanner(input1);
    			while (in1.hasNext() && i < vhcl.length)
    			{
    				vin = in1.nextLine();
    				make = in1.nextLine();
    				model = in1.nextLine();
    				year = in1.nextInt();
    				value = in1.nextDouble();
    				totalMilesDriven = in1.nextDouble();
    				lastOilChange = in1.nextDouble();
    				antiTheft = in1.nextBoolean();
    				stability = in1.nextBoolean();
    				passCap = in1.nextInt();
    				in1.nextLine();
    				vhcl[i] = new Vehicle(vin, make, model, year, value, 'c',
    						totalMilesDriven, lastOilChange);
     
    				i++;
    				numOfVehicles++;
    			}
    			in1 = new Scanner(input2);
    			while (in1.hasNext() && i < vhcl.length)
    			{
    				vin = in1.nextLine();
    				make = in1.nextLine();
    				model = in1.nextLine();
    				year = in1.nextInt();
    				value = in1.nextDouble();
    				totalMilesDriven = in1.nextDouble();
    				lastOilChange = in1.nextDouble();
    				antiTheft = in1.nextBoolean();
    				passCap = in1.nextInt();
    				in1.nextLine();
    				vhcl[i] = new Vehicle(vin, make, model, year, value, 'c',
    						totalMilesDriven, lastOilChange);
     
    				i++;
    				numOfVehicles++;
    			}
    			System.out.println(vhcl[i]);
    			System.out.println(numOfVehicles);
    		}
     
    		catch (FileNotFoundException e)
    		{
    			System.out.println("The file was not found.");
    		}
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Printing arrays

    How is the code executed for testing? I don't see a main() method
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Printing arrays

    Sorry, I forgot to post that.

    public class FleetApp
    {
    	public static void main(String[] args)
    	{
    		Fleet flt = new Fleet();
    		File input1 = new File("CarFleet.txt");
    		File input2 = new File("SUVFleet.txt");
    		flt.read(input1, input2);
    	}
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Printing arrays

    Also the code is missing the import statements and class definitions needed to compile the code.


    Also copy the full contents of the console window from when the code is executed and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Printing arrays

    import java.io.File;
     
    public class FleetApp
    {
    	public static void main(String[] args)
    	{
    		Fleet flt = new Fleet();
    		File input1 = new File("CarFleet.txt");
    		File input2 = new File("SUVFleet.txt");
    		flt.read(input1, input2);
    	}
    }

    When numOfVehicles is initialized to 0.

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at Fleet.read(Fleet.java:80)
    at FleetApp.main(FleetApp.java:32)

    When numOfVehicles is initialized to 10.

    null
    18

    --- Update ---

    I also have the import statements in the Vehicle class.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Printing arrays

    Still need the Vehicle class to compile and test.
    What are the contents of each of the files the program reads?

    The Scanner class needs an import.


    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at Fleet.read(Fleet.java:80)
    The error happened at line 80 in the Fleet class, but the Fleet class is less than 80 lines long????
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Printing arrays

    ok, here's all 3 updated classes. I have the contents of the files in my original post.
    The error in the fleet class is System.out.println(vhcl[i]);
    (I know the comments are off, I haven't worked on them yet)

    import java.io.PrintWriter;
    import java.util.Scanner;
     
    /*
     * Filename:  Vehicle.java
     * Create on Apr 21, 2013
     *
     * ULID: lmschoe
     * Lecture Section: 04
     * Instructor: Patricia Matsuda
     * Lab Section: 06
     * Instructor: Kay Bandela
     *
     */
     
    /**
     * <insert class description here>
     * 
     * @author Luke Schoenhofen
     * 
     */
    public class Vehicle
    {
    	private String vin, make, model;
    	private int year;
    	private double value, totalMilesDriven, lastOilChange;
    	private char type;
     
    	public Vehicle(String vin, String make, String model, int year, char type)
    	{
     
    	}
     
    	public Vehicle(String vin, String make, String model, int year,
    			double value, char type, double totalMilesDriven,
    			double lastOilChange)
    	{
     
    	}
     
    	public Vehicle(char type, Scanner in)
    	{
     
    	}
     
    	public String getVin()
    	{
    		return vin;
    	}
     
    	public String getMake()
    	{
    		return make;
    	}
     
    	public String getModel()
    	{
    		return model;
    	}
     
    	public int getYear()
    	{
    		return year;
    	}
     
    	public double getValue()
    	{
    		return value;
    	}
     
    	public double getTotalMilesDriven()
    	{
    		return totalMilesDriven;
    	}
     
    	public char getType()
    	{
    		return type;
    	}
     
    	public double getLastOilChange()
    	{
    		return lastOilChange;
    	}
     
    	public void setVin(String vin)
    	{
    		this.vin = vin;
    	}
     
    	public void setMake(String make)
    	{
    		this.make = make;
    	}
     
    	public void setModel(String model)
    	{
    		this.model = model;
    	}
     
    	public void setYear(int year)
    	{
    		this.year = year;
    	}
     
    	public void setValue(double value)
    	{
    		this.value = value;
    	}
     
    	public void setTotalMilesDriven(double totalMilesDriven)
    	{
    		this.totalMilesDriven = totalMilesDriven;
    	}
     
    	public void setLastOilChange(double lastOilChange)
    	{
    		this.lastOilChange = lastOilChange;
    	}
     
    	public void setType(char type)
    	{
    		this.type = type;
    	}
     
    	public void write(PrintWriter output)
    	{
    		output.println(vin + "\n" + make + "\n" + model + "\n" + year + " "
    				+ value + " " + totalMilesDriven + " " + lastOilChange);
    	}
     
    	public boolean needsOilChange()
    	{
    		return false;
    	}
     
    	public boolean needsNewTires()
    	{
    		return false;
    	}
     
    	public boolean shouldRetire()
    	{
    		return false;
    	}
     
    	public void update(double milesTraveled)
    	{
    		totalMilesDriven += milesTraveled;
    	}
     
    	public double depreciate(double per)
    	{
    		double dep;
     
    		per /= 100;
     
    		dep = per * value;
     
    		return dep;
    	}
     
    	public void record()
    	{
    		lastOilChange = getTotalMilesDriven();
    	}
     
    	public String toString()
    	{
    		String str;
     
    		str = "VIN: " + vin + "Make: " + make + "Model: " + model + "Year: "
    				+ year + "Value: " + value + "Miles Driven: "
    				+ totalMilesDriven + "Last Oil Change: " + lastOilChange
    				+ "Type: " + type;
     
    		return str;
    	}
    }


    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
     
    /*
     * Filename:  Fleet.java
     * Create on Apr 25, 2013
     *
     * ULID: lmschoe
     * Lecture Section: 04
     * Instructor: Patricia Matsuda
     * Lab Section: 06
     * Instructor: Kay Bandela
     *
     */
     
    /**
     * <insert class description here>
     * 
     * @author Luke Schoenhofen
     * 
     */
    public class Fleet
    {
    	Vehicle[] vhcl;
    	private int numOfVehicles = 10;
     
    	public Fleet()
    	{
    		vhcl = new Vehicle[numOfVehicles];
    	}
     
    	public void read(File input1, File input2)
    	{
    		String vin = null, make = null, model = null;
    		int year = 0, i = 0, ctr = 0, passCap;
    		double value = 0, totalMilesDriven = 0, lastOilChange = 0;
    		boolean antiTheft, stability;
    		try
    		{
    			Scanner in1 = new Scanner(input1);
    			while (in1.hasNext() && i < vhcl.length)
    			{
    				vin = in1.nextLine();
    				make = in1.nextLine();
    				model = in1.nextLine();
    				year = in1.nextInt();
    				value = in1.nextDouble();
    				totalMilesDriven = in1.nextDouble();
    				lastOilChange = in1.nextDouble();
    				antiTheft = in1.nextBoolean();
    				stability = in1.nextBoolean();
    				passCap = in1.nextInt();
    				in1.nextLine();
    				vhcl[i] = new Vehicle(vin, make, model, year, value, 'c',
    						totalMilesDriven, lastOilChange);
     
    				i++;
    				numOfVehicles++;
    			}
    			in1 = new Scanner(input2);
    			while (in1.hasNext() && i < vhcl.length)
    			{
    				vin = in1.nextLine();
    				make = in1.nextLine();
    				model = in1.nextLine();
    				year = in1.nextInt();
    				value = in1.nextDouble();
    				totalMilesDriven = in1.nextDouble();
    				lastOilChange = in1.nextDouble();
    				antiTheft = in1.nextBoolean();
    				passCap = in1.nextInt();
    				in1.nextLine();
    				vhcl[i] = new Vehicle(vin, make, model, year, value, 'c',
    						totalMilesDriven, lastOilChange);
     
    				i++;
    				numOfVehicles++;
    			}
    			System.out.println(vhcl[i]);
    			System.out.println(numOfVehicles);
    		}
     
    		catch (FileNotFoundException e)
    		{
    			System.out.println("The file was not found.");
    		}
    	}
    }


    import java.io.File;
     
    /*
     * Filename:  FleetApp.java
     * Create on Apr 25, 2013
     *
     * ULID: lmschoe
     * Lecture Section: 04
     * Instructor: Patricia Matsuda
     * Lab Section: 06
     * Instructor: Kay Bandela
     *
     */
     
    /**
     * <insert class description here>
     * 
     * @author Luke Schoenhofen
     * 
     */
    public class FleetApp
    {
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args)
    	{
    		Fleet flt = new Fleet();
    		File input1 = new File("CarFleet.txt");
    		File input2 = new File("SUVFleet.txt");
    		flt.read(input1, input2);
    	}
     
    }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Printing arrays

    What are the contents of each of the files the program reads?
    			System.out.println(vhcl[i]);
    			System.out.println(numOfVehicles);
    What do you expect to be printed by these two statements?
    What is the variable: numOfVehicles supposed to contain?
    If you initialize it to 10, then it's value will reflect that starting value.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Apr 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Printing arrays

    I expect it to print out exactly the way it come in. So like this:

    1256BB03457
    Hyundai
    Santa Fe
    2011 18556.0 31195 27301
    true true 5
    5XXCD3AX66
    Kia
    Sorento
    2005 9400.0 67124 60245
    false false 5
    9SEF3AX56M
    FORD
    TAURUS 3.5L
    2001 5400.0 53892 50753
    false true 5
    59867451W
    Honda
    Odyssey Touring Elite
    2011 35000.0 7500 7500
    true true 7

    ADF2462622F
    Chevrolet
    Tahoe
    2007 35000.0 35194 24301
    false 9
    B288413D444
    Ford
    Expedition
    2013 43000.0 5500 3500
    false 8
    GKS61162226
    GMC
    Yukon Hybrid
    2004 51000.0 210000 209000
    true 5
    7865K162563
    Toyota
    Sequoia
    2004 51000.0 210000 209000
    false 8

    numOfVehicles is the number of vehicles that I read from the 2 files, or the number of objects in the array. So it should be 8 after it reads the files.
    I realize that initializing numOfVehicles to ten means that is the starting value, I'm just confused on how if I initialize it to 0, it will increment, but I still get the java.lang.ArrayIndexOutOfBoundsException: 0 error.

    --- Update ---

    Actually, shouldn't it be using the toString method from the Vehicle class?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Printing arrays

    			System.out.println(vhcl[i]);    //????? What is in this slot in the array?
    			System.out.println(numOfVehicles);
    What do you expect to be printed by these two statements?
    What is the variable: numOfVehicles supposed to contain?
    If you initialize it to 10, then it's value will reflect that starting value.

    numOfVehicles is the number of vehicles that I read from the 2 files, or the number of objects in the array. So it should be 8 after it reads the files.
    It does not start at 0:
    private int numOfVehicles = 10;


    If you define the array to be much larger that all the number of objects that are put into it, there won't be an ArrayIndexOutOfBoundsException. Say 300 or 1000.
    Use the numOfVehicles to count the number of objects that have been put into the array and later to control all array references that search through the array.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Apr 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Printing arrays

    System.out.println(vhcl[i]);

    Honestly, I don't really know what it in that slot in the array, I think the last index the scanner reads. I realize now that I am trying to use the toString method in the Vehicle class to print the contents of the array, and I'm not sure how to do it.

    private int numOfVehicles = 10;

    Ok, say I initialize numOfVehicles to 0. Why is there an error if I am incrementing numOfVehicles after it reads one in? Shouldn't that set the array to hold 8 objects?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Printing arrays

    Post the new version of the code. It should define a large enough array and use the numOfVehicles variable to count the Vehicles as they are added.

    how to do it.
    For testing use an index of 0 to show the first object in the array:
    System.out.println(vhcl[0]);
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Apr 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Printing arrays

    Do you mean just to initialize it to ten or something like that? Because then numOfVehicles will print as 18 when it should be 8.

    import java.io.PrintWriter;
    import java.util.Scanner;
     
    /*
     * Filename:  Vehicle.java
     * Create on Apr 21, 2013
     *
     * ULID: lmschoe
     * Lecture Section: 04
     * Instructor: Patricia Matsuda
     * Lab Section: 06
     * Instructor: Kay Bandela
     *
     */
     
    /**
     * <insert class description here>
     * 
     * @author Luke Schoenhofen
     * 
     */
    public class Vehicle
    {
    	private String vin, make, model;
    	private int year;
    	private double value, totalMilesDriven, lastOilChange;
    	private char type;
     
    	public Vehicle(String vin, String make, String model, int year, char type)
    	{
     
    	}
     
    	public Vehicle(String vin, String make, String model, int year,
    			double value, char type, double totalMilesDriven,
    			double lastOilChange)
    	{
     
    	}
     
    	public Vehicle(char type, Scanner in)
    	{
     
    	}
     
    	public String getVin()
    	{
    		return vin;
    	}
     
    	public String getMake()
    	{
    		return make;
    	}
     
    	public String getModel()
    	{
    		return model;
    	}
     
    	public int getYear()
    	{
    		return year;
    	}
     
    	public double getValue()
    	{
    		return value;
    	}
     
    	public double getTotalMilesDriven()
    	{
    		return totalMilesDriven;
    	}
     
    	public char getType()
    	{
    		return type;
    	}
     
    	public double getLastOilChange()
    	{
    		return lastOilChange;
    	}
     
    	public void setVin(String vin)
    	{
    		this.vin = vin;
    	}
     
    	public void setMake(String make)
    	{
    		this.make = make;
    	}
     
    	public void setModel(String model)
    	{
    		this.model = model;
    	}
     
    	public void setYear(int year)
    	{
    		this.year = year;
    	}
     
    	public void setValue(double value)
    	{
    		this.value = value;
    	}
     
    	public void setTotalMilesDriven(double totalMilesDriven)
    	{
    		this.totalMilesDriven = totalMilesDriven;
    	}
     
    	public void setLastOilChange(double lastOilChange)
    	{
    		this.lastOilChange = lastOilChange;
    	}
     
    	public void setType(char type)
    	{
    		this.type = type;
    	}
     
    	public void write(PrintWriter output)
    	{
    		output.println(vin + "\n" + make + "\n" + model + "\n" + year + " "
    				+ value + " " + totalMilesDriven + " " + lastOilChange);
    	}
     
    	public boolean needsOilChange()
    	{
    		return false;
    	}
     
    	public boolean needsNewTires()
    	{
    		return false;
    	}
     
    	public boolean shouldRetire()
    	{
    		return false;
    	}
     
    	public void update(double milesTraveled)
    	{
    		totalMilesDriven += milesTraveled;
    	}
     
    	public double depreciate(double per)
    	{
    		double dep;
     
    		per /= 100;
     
    		dep = per * value;
     
    		return dep;
    	}
     
    	public void record()
    	{
    		lastOilChange = getTotalMilesDriven();
    	}
     
    	public String toString()
    	{
    		String str;
     
    		str = "VIN: " + vin + "\nMake: " + make + "\nModel: " + model
    				+ "\nYear: " + year + " Value: " + value + " Miles Driven: "
    				+ totalMilesDriven + " Last Oil Change: " + lastOilChange
    				+ " Type: " + type;
     
    		return str;
    	}
    }

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
     
    /*
     * Filename:  Fleet.java
     * Create on Apr 25, 2013
     *
     * ULID: lmschoe
     * Lecture Section: 04
     * Instructor: Patricia Matsuda
     * Lab Section: 06
     * Instructor: Kay Bandela
     *
     */
     
    /**
     * <insert class description here>
     * 
     * @author Luke Schoenhofen
     * 
     */
    public class Fleet
    {
    	Vehicle[] vhcl;
    	private int numOfVehicles = 10;
     
    	public Fleet()
    	{
    		vhcl = new Vehicle[numOfVehicles];
    	}
     
    	public void read(File input1, File input2)
    	{
    		String vin = null, make = null, model = null;
    		int year = 0, i = 0, ctr = 0, passCap;
    		double value = 0, totalMilesDriven = 0, lastOilChange = 0;
    		boolean antiTheft, stability;
    		try
    		{
    			Scanner in1 = new Scanner(input1);
    			while (in1.hasNext() && i < vhcl.length)
    			{
    				vin = in1.nextLine();
    				make = in1.nextLine();
    				model = in1.nextLine();
    				year = in1.nextInt();
    				value = in1.nextDouble();
    				totalMilesDriven = in1.nextDouble();
    				lastOilChange = in1.nextDouble();
    				antiTheft = in1.nextBoolean();
    				stability = in1.nextBoolean();
    				passCap = in1.nextInt();
    				in1.nextLine();
    				vhcl[i] = new Vehicle(vin, make, model, year, value, 'c',
    						totalMilesDriven, lastOilChange);
     
    				i++;
    				numOfVehicles++;
    			}
    			in1 = new Scanner(input2);
    			while (in1.hasNext() && i < vhcl.length)
    			{
    				vin = in1.nextLine();
    				make = in1.nextLine();
    				model = in1.nextLine();
    				year = in1.nextInt();
    				value = in1.nextDouble();
    				totalMilesDriven = in1.nextDouble();
    				lastOilChange = in1.nextDouble();
    				antiTheft = in1.nextBoolean();
    				passCap = in1.nextInt();
    				in1.nextLine();
    				vhcl[i] = new Vehicle(vin, make, model, year, value, 'c',
    						totalMilesDriven, lastOilChange);
     
    				i++;
    				numOfVehicles++;
    			}
    			System.out.println(vhcl[0]);
    			System.out.println(numOfVehicles);
    		}
     
    		catch (FileNotFoundException e)
    		{
    			System.out.println("The file was not found.");
    		}
    	}
    }

    import java.io.File;
     
    /*
     * Filename:  FleetApp.java
     * Create on Apr 25, 2013
     *
     * ULID: lmschoe
     * Lecture Section: 04
     * Instructor: Patricia Matsuda
     * Lab Section: 06
     * Instructor: Kay Bandela
     *
     */
     
    /**
     * <insert class description here>
     * 
     * @author Luke Schoenhofen
     * 
     */
    public class FleetApp
    {
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args)
    	{
    		Fleet flt = new Fleet();
    		File input1 = new File("CarFleet.txt");
    		File input2 = new File("SUVFleet.txt");
    		flt.read(input1, input2);
    	}
    }

    Here's what it prints:

    VIN: null
    Make: null
    Model: null
    Year: 0 Value: 0.0 Miles Driven: 0.0 Last Oil Change: 0.0 Type:
    18

    Why is it not printing with the assigned values from the files it reads?

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Printing arrays

    Do you mean just to initialize it to ten or something like that?
    No. Initialize numOfVehicles to 0
    Define the Vehicle array with enough slots to hold the most possible data: 300 or 1000. This is a problem with arrays that will go away when you learn to use ArrayLists

    VIN: null
    Make: null
    Model: null
    Year: 0 Value: 0.0 Miles Driven: 0.0 Last Oil Change: 0.0 Type:
    Look at the Vehicle class to see why the class variables do not get assigned values.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Apr 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Printing arrays

    I defined the array to hold 100 in the constructor in the Fleet class. Is that right? It works correctly.
    I the Vehicle class I set the parameters equal to the variables and that worked also.

    import java.io.PrintWriter;
    import java.util.Scanner;
     
    /*
     * Filename:  Vehicle.java
     * Create on Apr 21, 2013
     *
     * ULID: lmschoe
     * Lecture Section: 04
     * Instructor: Patricia Matsuda
     * Lab Section: 06
     * Instructor: Kay Bandela
     *
     */
     
    /**
     * <insert class description here>
     * 
     * @author Luke Schoenhofen
     * 
     */
    public class Vehicle
    {
    	private String vin, make, model;
    	private int year;
    	private double value, totalMilesDriven, lastOilChange;
    	private char type;
     
    	public Vehicle(String vin, String make, String model, int year, char type)
    	{
    		this.vin = vin;
    		this.make = make;
    		this.model = model;
    		this.year = year;
    		this.type = type;
    	}
     
    	public Vehicle(String vin, String make, String model, int year,
    			double value, char type, double totalMilesDriven,
    			double lastOilChange)
    	{
    		this.vin = vin;
    		this.make = make;
    		this.model = model;
    		this.year = year;
    		this.value = value;
    		this.type = type;
    		this.totalMilesDriven = totalMilesDriven;
    		this.lastOilChange = lastOilChange;
    	}
     
    	public Vehicle(char type, Scanner in)
    	{
    		this.type = type;
    	}
     
    	public String getVin()
    	{
    		return vin;
    	}
     
    	public String getMake()
    	{
    		return make;
    	}
     
    	public String getModel()
    	{
    		return model;
    	}
     
    	public int getYear()
    	{
    		return year;
    	}
     
    	public double getValue()
    	{
    		return value;
    	}
     
    	public double getTotalMilesDriven()
    	{
    		return totalMilesDriven;
    	}
     
    	public char getType()
    	{
    		return type;
    	}
     
    	public double getLastOilChange()
    	{
    		return lastOilChange;
    	}
     
    	public void setVin(String vin)
    	{
    		this.vin = vin;
    	}
     
    	public void setMake(String make)
    	{
    		this.make = make;
    	}
     
    	public void setModel(String model)
    	{
    		this.model = model;
    	}
     
    	public void setYear(int year)
    	{
    		this.year = year;
    	}
     
    	public void setValue(double value)
    	{
    		this.value = value;
    	}
     
    	public void setTotalMilesDriven(double totalMilesDriven)
    	{
    		this.totalMilesDriven = totalMilesDriven;
    	}
     
    	public void setLastOilChange(double lastOilChange)
    	{
    		this.lastOilChange = lastOilChange;
    	}
     
    	public void setType(char type)
    	{
    		this.type = type;
    	}
     
    	public void write(PrintWriter output)
    	{
    		output.println(vin + "\n" + make + "\n" + model + "\n" + year + " "
    				+ value + " " + totalMilesDriven + " " + lastOilChange);
    	}
     
    	public boolean needsOilChange()
    	{
    		return false;
    	}
     
    	public boolean needsNewTires()
    	{
    		return false;
    	}
     
    	public boolean shouldRetire()
    	{
    		return false;
    	}
     
    	public void update(double milesTraveled)
    	{
    		totalMilesDriven += milesTraveled;
    	}
     
    	public double depreciate(double per)
    	{
    		double dep;
     
    		per /= 100;
     
    		dep = per * value;
     
    		return dep;
    	}
     
    	public void record()
    	{
    		lastOilChange = getTotalMilesDriven();
    	}
     
    	public String toString()
    	{
    		String str;
     
    		str = "VIN: " + vin + "\nMake: " + make + "\nModel: " + model
    				+ "\nYear: " + year + " Value: " + value + " Miles Driven: "
    				+ totalMilesDriven + " Last Oil Change: " + lastOilChange
    				+ " Type: " + type;
     
    		return str;
    	}
    }

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
     
    /*
     * Filename:  Fleet.java
     * Create on Apr 25, 2013
     *
     * ULID: lmschoe
     * Lecture Section: 04
     * Instructor: Patricia Matsuda
     * Lab Section: 06
     * Instructor: Kay Bandela
     *
     */
     
    /**
     * <insert class description here>
     * 
     * @author Luke Schoenhofen
     * 
     */
    public class Fleet
    {
    	Vehicle[] vhcl;
    	private int numOfVehicles = 0;
     
    	public Fleet()
    	{
    		vhcl = new Vehicle[100];
    	}
     
    	public void read(File input1, File input2)
    	{
    		String vin = null, make = null, model = null;
    		int year = 0, i = 0, ctr = 0, passCap;
    		double value = 0, totalMilesDriven = 0, lastOilChange = 0;
    		boolean antiTheft, stability;
    		try
    		{
    			Scanner in1 = new Scanner(input1);
    			while (in1.hasNext() && i < vhcl.length)
    			{
    				vin = in1.nextLine();
    				make = in1.nextLine();
    				model = in1.nextLine();
    				year = in1.nextInt();
    				value = in1.nextDouble();
    				totalMilesDriven = in1.nextDouble();
    				lastOilChange = in1.nextDouble();
    				antiTheft = in1.nextBoolean();
    				stability = in1.nextBoolean();
    				passCap = in1.nextInt();
    				in1.nextLine();
    				vhcl[i] = new Vehicle(vin, make, model, year, value, 'c',
    						totalMilesDriven, lastOilChange);
     
    				i++;
    				numOfVehicles++;
    			}
    			in1 = new Scanner(input2);
    			while (in1.hasNext() && i < vhcl.length)
    			{
    				vin = in1.nextLine();
    				make = in1.nextLine();
    				model = in1.nextLine();
    				year = in1.nextInt();
    				value = in1.nextDouble();
    				totalMilesDriven = in1.nextDouble();
    				lastOilChange = in1.nextDouble();
    				antiTheft = in1.nextBoolean();
    				passCap = in1.nextInt();
    				in1.nextLine();
    				vhcl[i] = new Vehicle(vin, make, model, year, value, 's',
    						totalMilesDriven, lastOilChange);
     
    				i++;
    				numOfVehicles++;
    			}
    			System.out.println(vhcl[0]);
    			System.out.println(numOfVehicles);
    		}
     
    		catch (FileNotFoundException e)
    		{
    			System.out.println("The file was not found.");
    		}
    	}
    }


    Here's what it prints:

    VIN: 1256BB03457
    Make: Hyundai
    Model: Santa Fe
    Year: 2011 Value: 18556.0 Miles Driven: 31195.0 Last Oil Change: 27301.0 Type: c
    8


    One last question. Do I have to do this:
    System.out.println(vhcl[0]);
    System.out.println(vhcl[1]);
    System.out.println(vhcl[2]);
    System.out.println(vhcl[3]);
    System.out.println(vhcl[4]);
    System.out.println(vhcl[5]);
    System.out.println(vhcl[6]);
    System.out.println(vhcl[7]);

    to print every object?

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Printing arrays

    Look at all those statements that have an index goes from 0 to 7. That looks like the code should use a for loop and have the code use the loop's variable as an index into the array. The loop should use the value of the numOfVehicles variable to know when to stop looping.
    If you don't understand my answer, don't ignore it, ask a question.

  17. The Following User Says Thank You to Norm For This Useful Post:

    polop050505 (April 26th, 2013)

  18. #17
    Junior Member
    Join Date
    Apr 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Printing arrays

    I did that and it works.
    Thank you very much, you were very helpful.

  19. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Printing arrays

    Glad you got it working, Please mark this as solved. Use Thread Tools at top
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Printing arrays from user inputs (2 dimensional)
    By kevinsauerzxc in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 13th, 2013, 07:28 AM
  2. printing distinct numbers with arrays
    By ColeTrain in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 16th, 2012, 08:32 AM
  3. Counting a total and printing value from 2 arrays
    By stewart86 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 2nd, 2012, 07:25 AM
  4. Replies: 1
    Last Post: September 28th, 2011, 07:29 AM
  5. Printing a Histogram Help - Arrays
    By Mock26 in forum Collections and Generics
    Replies: 1
    Last Post: June 4th, 2009, 04:49 AM

Tags for this Thread