-1

I'm doing a little application where I'm moving around some circles, squares and triangles. Which coordinates I read from a txt file. But once I'm done moving them I'd like to save their coordinates in the same txt file.

This is how the code looks like right now:

import java.io.FileNotFoundException;
import se.lth.cs.ptdc.window.SimpleWindow;

public class ShapeTest {

    public static void main(String[] args) throws FileNotFoundException {
            SimpleWindow w = new SimpleWindow(600, 600, "ShapeTest");
            ShapeList shapes = new ShapeList();
            java.util.Scanner scan = null;
            try {
                scan = new java.util.Scanner(new java.io.File("shapedata.txt"));
            } catch (java.io.FileNotFoundException e) {
                System.err.println("shapedata.txt couldn't be found");
            }

            int x,y,z;
            while(scan.hasNext()) {
                    String s = scan.next();
                    if (s.contentEquals("S")){
                            x = scan.nextInt();
                            y = scan.nextInt();
                            z = scan.nextInt();
                            shapes.insert(new Square(x,y,z));
                    } else if (s.contentEquals("C")) {
                            x = scan.nextInt();
                            y = scan.nextInt();
                            z = scan.nextInt();
                            shapes.insert(new Circle(x,y,z));
                    } else if (s.contentEquals("T")) {
                            x = scan.nextInt();
                            y = scan.nextInt();
                            z = scan.nextInt();
                            shapes.insert(new Triangle(x,y,z));
                    }

            }
            shapes.draw(w);

            CommandDispatcher cd = new CommandDispatcher(w,shapes);
            cd.mainLoop();
    }
}

What would I need to add? I tried FileUtils.writeStringToFile without any good result.

Dante May Code
  • 11,177
  • 9
  • 49
  • 81
Michael
  • 644
  • 5
  • 14
  • 33
  • 1
    What is your problem with `FileUtils.writeStringToFile`? – rekire Dec 07 '12 at 09:02
  • Is the `String` in `SimpleWindow` inside a `JTextComponent` that is accessible? – Andrew Thompson Dec 07 '12 at 09:03
  • Rekire, no idea but I didn't get it to work properly. Perhaps I put it at a wrong place. – Michael Dec 07 '12 at 09:05
  • 1
    Where did you place when it was not working? i don't see it (`FileUtile.writeStringToFile`) in your code – acostache Dec 07 '12 at 09:11
  • create a file output stream and write your coordinates to the file whats the problem? – Bhavik Shah Dec 07 '12 at 09:25
  • I guess Square, Circle and Triangle are all subclassing a class named Shape. Therefore each of them have at least 4 attributes: coords x,y,z and an identifier char. So you simply have to overwrite e.g. toString() (or define your own method, then again in Shape) for the String representation of that object. After that save the ShapeList to a Textfile e.g. with FileUtils (use library from http://commons.apache.org/) or do it yourself. – zip Dec 07 '12 at 10:33

2 Answers2

0

You can try to use java.util.Formatter to verify your text file and then, format your outputs, store them in the same text file.

Venzentx
  • 487
  • 3
  • 6
  • 17
0

If you are having trouble with saving the ints, just use String.valueOf() to convert the values into a string and save them normally. Then you can use Integer.parseInt() to read them back.

If you're asking how to save to a file in general, that's a larger question and answers more qualified than mine are available here on Stack Overflow.

Community
  • 1
  • 1
Scott Forsythe
  • 360
  • 6
  • 18