Wrting an array of objects to text file

Wrting an array of objects to text file

Page 1 of 1

6 Replies - 8445 Views - Last Post: 13 June 2010 - 09:00 AM Rate Topic: -----

#1 bymak87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-June 10

Wrting an array of objects to text file

Posted 13 June 2010 - 07:15 AM

I had to write 10 objects of cars into an array and now my problem now is writing the array into a text file. I am able to get the correct output in the console, but when I open the text file I get part of the desired output, as well as a combination of letters and numbers. This is an example of the desired output:

The brand of the car is a BMW.
The color of the car is silver.
The mileage of the car is 25000 miles.
The gas tank is full.
You(don't want) to fill up the gas tank.

But here is what I get:

BMW@a62fc3

I know my problem is in this line:
output.write(x +System.getProperty("line.separator"));
I'm not sure what I'm supposed to put instead of x. I have tried thelist and the same output comes out.

Any help or advice would be greatly appreciated!!

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;


public class P10 {
	public static void main (String args[])throws IOException
{
		
		Info [] thelist = new Info [10];
		Mazda m = new Mazda();
		Ford f =  new Ford();
		Kia k = new Kia ();
		Dodge d = new Dodge();
		Mercedes r = new Mercedes ();
		Chrysler c = new Chrysler ();
		Jeep j = new Jeep();
		Hyundai h = new Hyundai();
		Jaguar g = new Jaguar ();
		BMW b= new BMW ();
		
		thelist[0] = m;
		thelist[1] = f;
		thelist[2] = k;
		thelist[3] = d;
		thelist[4] = r;
		thelist[5] = c;
		thelist[6] = j;
		thelist[7] = h;
		thelist[8] = g;
		thelist[9] = b;
		try {
			   Writer output = null;
			    String text = "This is the information on the cars:";//text has been assigned
			    File i = new File("Information.txt");//name of txt file
			    output = new BufferedWriter(new FileWriter(i));
			    output.write(text);//text is output
			    System.out.println("Your file has been written to " + (i.getName()+ "."));		
		for (Info x: thelist){
			x.Brand();
			x.Color();
			x.Mileage();
			x.Gas();
			x.Answer();
			System.out.println();
			output.write(System.getProperty("line.separator"));//blank space
			 output.write(x +System.getProperty("line.separator"));//output is the array of car objects
			 
			}//ends for
			output.close();//ends output
			
		   }//ends try 
		   catch (IOException e) {// if there is no file
				System.err.println("The file was not saved.");//error message
				e.printStackTrace();
				}//ends catch
		}
	}




Is This A Good Question/Topic? 0
  • +

Replies To: Wrting an array of objects to text file

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Wrting an array of objects to text file

Posted 13 June 2010 - 07:24 AM

do your classes have toString methods??..you're getting weird output because you are trying to print the address of the object..anyway if you want to just send the whole object to the file then you better serialize it, otherwise you will have to print each instance variable separately to the file

This post has been edited by mostyfriedman: 13 June 2010 - 07:29 AM

Was This Post Helpful? 1
  • +
  • -

#3 bymak87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-June 10

Re: Wrting an array of objects to text file

Posted 13 June 2010 - 07:28 AM

No. I don't think they do.
This is an example of my classes
public class BMW extends Info {
	public void Brand(){
		 System.out.println("The brand of the car is a BMW.");
	}
	public void Color(){
		 System.out.println("The color of the car is silver.");
	 }
	 public void Mileage(){
		 System.out.println("The mileage of the car is 25000 miles.");
	 }
	 public void Gas(){
		 System.out.println("The gas tank is full.");
	
	 }
	 public void Answer(){
		 System.out.println("You(don't want) to fill up the gas tank. ");
	 }
}

This post has been edited by bymak87: 13 June 2010 - 07:34 AM

Was This Post Helpful? 0
  • +
  • -

#4 bymak87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-June 10

Re: Wrting an array of objects to text file

Posted 13 June 2010 - 07:36 AM

When you say serialize, do you mean name the array?
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10662
  • View blog
  • Posts: 39,590
  • Joined: 27-December 08

Re: Wrting an array of objects to text file

Posted 13 June 2010 - 07:48 AM

I agree with mostyfriedman that Serialization is the better design choice here. However, it's a little more advanced, so maybe you don't want to go there just yet.

First, some things I noticed about your design is that you are subclassing for each brand of car. Why don't you just design a car class with attributes brand, tankCapacity, amountInTank, mpg, color, etc? Then have getter and setter methods for each of these attributes, with the getter methods returning these attributes rather than printing them to the console. Then, override the toString() method inherited from Object, and whatever String you want to be displayed when the object is printed, define toString() to return in that format. In this way, you can dump your Object to the console and not get the memory address.

//an example with mileage
class Car{ 
   private double mileage;
 
   //notice how getMileage() returns the mileage
   //instead of printing it. This gives you more control
   //for modifying or working with the value for mileage
   public double getMileage(){return mileage;}
   public double setMileage(double mileage){this.mileage = mileage;}
}


Was This Post Helpful? 1
  • +
  • -

#6 bymak87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-June 10

Re: Wrting an array of objects to text file

Posted 13 June 2010 - 08:04 AM

Thanks for the advice. I'm going to rewrite everything.
Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2779
  • View blog
  • Posts: 11,763
  • Joined: 20-September 08

Re: Wrting an array of objects to text file

Posted 13 June 2010 - 09:00 AM

macosxnerd101 has made an important point about not using subclasses for each brand of car.

You should look into XMLEncoder too, it makes serialization pretty simple and will give you human-readable output
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1