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:


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){
			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
				}//ends catch
6 Years
Discussion Span
Last Post by JamesCherrill

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.