0

I would like to write out the values of my array to a file.

public void Write(String fileName) throws IOException
{

    PrintWriter outFile;
    outFile = new PrintWriter(new FileWriter(fileName));

    for(int K = 0 ; K < CurrentCount ; K++)
        outFile.println(List[K].toString());

    outFile.close();
}

The data entered into the file was: WeatherStation@1194a4e

The WeatherStation class has my constructor and get and set methods

 List[K] = new WeatherStation(Location, Temp, Title, Air, Pollen);

I was expecting all of the attributes above to get placed into the file.

Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
5hits
  • 3
  • 1
  • Possible duplicate of [How to support println in a class?](http://stackoverflow.com/questions/27647567/how-to-support-println-in-a-class) – Raedwald Mar 26 '16 at 14:09
  • Possible duplicate of http://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4 – Raedwald Mar 26 '16 at 14:19

3 Answers3

3

If you expect that, then you will need to override toString for your WeatherStation class.

The default is a very simple one which outputs the type and a unique ID (not sure whether it's an address or hash off the top of my head).

Simply add a toString method which goes through the members off your class (that you want printed) and concatenate them into a string.

This link here has a good example on how to do this. You'd do something like:

@Override public String toString() {
    StringBuilder result = new StringBuilder();

    result.append (this.getClass().getName() + " {");
    result.append (" Location=[" + this.Location + "]");
    result.append (", Temp=[" + this.Temp + "]");
    // Other fields.
    result.append (", Pollen=[" + this.Pollen + "]");
    result.append (" }");

    return result.toString();
  }

It's untested and probably not the exact format you want, but it should be a good start.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Instead of using .toString() I just listed out outFile.println(List[K].GetLocation()); outFile.println(List[K].GetTemp()); etc.. Any Objection to that? – 5hits Mar 11 '11 at 01:12
  • @5hits, only insofar as it's not really the OO way to do it. Don't get me wrong, if that works for you, go for it. Just keep in mind that it solves a very _narrow_ problem. What if some other part of your code needs a string representation? What if you change the internals of the class and then have to go and change your printing code in a hundred different places? With OO, objects should ideally maintain their own data _and_ code, so that everything is localised. I would personally rather see the problem solved completely but sometimes pragmatism beats purity :-) – paxdiablo Mar 11 '11 at 05:08
1

You would need to override toString() in your WeatherStation class to produce the output you want.

Brian Roach
  • 76,169
  • 12
  • 136
  • 161
1

Simplify your life!

  • a) There is no problem with initializing on declaration.
  • b) The simplified for-loop is - well - simplified.
  • c) If you don't pass a primitive or String to println, .toString () will be called automatically.

compare yourself:

public void Write(String fileName) throws IOException
{
    PrintWriter outFile = new PrintWriter (new FileWriter (fileName));
    for (WheatherStation ws : List)
        outFile.println (ws);
    outFile.close();
}

And - did anybody tell you to overwrite 'toString'? ;) I agree.

jzd
  • 23,473
  • 9
  • 54
  • 76
user unknown
  • 35,537
  • 11
  • 75
  • 121