PrintWriter - not writing correctly

Hi,

I'm having an issue with trying to write a String[] to a .txt file using PrintWriter

I have a string, and when I println it to the console it comes up grand, but when I write it to the .txt file it just comes up like '[Ljava.lang.String;@42e95cbf' or something . I've tried writing other Strings to the .txt file it works fine, but I just cannot write this particular string

  String[] lines = markov.generateSentences(3);

  RiText.createLines(this, lines, 50, 50, 400, 400);

  println(lines); //this prints to the console file
  output.println("im trying to print the string lines to the output txt file " + lines);
  output.flush();
  output.close();

However, in my .txt file I'm getting this:

im trying to print the string lines to the output txt file [Ljava.lang.String;@6c029886

I assume that it has something to do with the [] part of the string 'lines'.

Any ideas? As to what the issue is?

Tagged:

Answers

  • Answer ✓

    Processing's own println() knows how to extract information from arrays.
    Also it automatically invokes toString() when dealing w/ other objects.

    What you can do is manually iterate over the array:

    final String[] lines = { "print","me","now!" };
    for (String s: lines)  println(s);
    exit();
    
  • Hi,

    That worked perfectly. I'll have to look up the toString() method.

    Thanks very much for your help :)

  • All objects have method toString()! But we have to override it w/ something meaningful! :ar!

  • Oh man. I have a lot to learn!

Sign In or Register to comment.