Okay following I have already posted a discussion like this but I got a website as an answer and the website had some coding, so I copied everything and inserted it on the right spot but now it still doesn't create a .txt file. After I thought for a little I decided to make the class that has the coding spawn using the addObject code but it told me I can not use it in this context so what should I do in order to create .txt files using Greenfoot??? Also the class I made is in a new over all class called "Other classes".
By the way here is the coding:
If I try using the addObject code it tells me this error:
addObject(greenfoot.Actor,int,int) in greenfoot.World cannot be applied to (PrintWriterExample,int,int)
Can you guys help me?!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; public class PrintWriterExample { public static void main(String args) { String filename = "c:" + File.separator + "JustExample.txt" ; File f = new File(filename); PrintWriter pw = null ; try { pw = new PrintWriter(f); String strContent = "Just Example" ; int intContent = 1 ; double doubleContent = Math.random(); //convinient way to add new line while print content pw.println(strContent); //using printf to format content. SInce java 1.5 pw.printf( "Hello this is %s. I am %d years old. My lucky number is %f" , strContent, intContent, doubleContent); pw.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } finally { //no matter what happen. close the output stream always. //note that closing a printer will not throw IOException if (pw!= null ){ pw.close(); } } } } |