954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Wrting an array of objects to text file

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:
50. 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
		
	}
}
bymak87
Newbie Poster
7 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

What do the methods like x.Brand(); do?

BMW@a62fc3 is what the public String toString() method produces when it's inherited from Object. Many Java API methods call toString() when they need a textual representation of an object (eg System.out.print(...)). It's normal practice to override toString() in any new class you define. You should return a String that is how you would like to see an instance of that class printed.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: