We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I am currently trying to write data to a .txt file.
At start it seems to go well and the strings are written correctly when checking with Notepad. as shown bellow (the output in Notepad++ is the same data but with wrong layout.
I use a Printwriter and an ArrayList in order to write the data. I am unsure how to resolve the issue. It has to do with the fact i read data as Strings, reading the data as floats didnt help so far
a step in the correct direction would be of great help.
my code:
XML xml;
ArrayList<String> numbers;
void setup() {
// gbxml nu eerst in notepad geladen en opgeslagen om BOM eruit te krijgen (handmatig nu nog)
// opslaan notepad via ANSI formaat (wel als .xml) werkt.
xml = loadXML("OA_4.xml");
PrintWriter output = createWriter("hoppa.txt");
numbers = new ArrayList<String>();
///////////////////////////////////////////////////
//////////////////////////////////////////////////
/////data naam voor solids & windows
//////////////////////////////////////////////////
//////////////////////////////////////////////////
XML[] namesolids= xml.getChildren("Campus/Surface/CADObjectId");
for (int i=0; i<namesolids.length;i++){
// println(namesolids[i].getChildren());
}
XML[] namewindows= xml.getChildren("Campus/Surface/Opening/CADObjectId");
for (int i=0; i<namewindows.length;i++){
// println(namewindows[i].getChildren());
}
///////////////////////////////////////////////////
//////////////////////////////////////////////////
/////data voor dak/vloer/wanden voorbereiden
//////////////////////////////////////////////////
//////////////////////////////////////////////////
XML[] datasolids= xml.getChildren("Campus/Surface/PlanarGeometry/PolyLoop/CartesianPoint");
int lengtesolids=datasolids.length;
// een array maken die gevuld met coordinaten kan worden.
// De array stelt de data voor wanden,vloeren en plafonds voor.
String Solids [];
Solids= new String [lengtesolids];
for (int i=0; i<lengtesolids; i++){
numbers.add( new String(datasolids[i].getContent("Coordinate")));
//println(numbers.get(i));
}
for (int i=0; i<namesolids.length;i++){
output.println("Solid polygon");
output.println("0");
output.println("0");
output.println("12");
for (int j=0; j<4;j++){
output.println(numbers.get(j));
println(numbers.get(j));
}
output.println("");
output.println("");
numbers.remove(0);
numbers.remove(0);
numbers.remove(0);
numbers.remove(0);
}
output.flush(); // Write the remaining data
output.close(); // Finish the file
exit(); // Stop the program
// output.flush();
// output.close();
///////////////////////////////////////////////////
//////////////////////////////////////////////////
/////data voor ramen voorbereiden
//////////////////////////////////////////////////
//////////////////////////////////////////////////
XML[] datawindows= xml.getChildren("Campus/Surface/Opening/PlanarGeometry/PolyLoop/CartesianPoint");
int lengtewindows=datawindows.length;
// een array maken die gevuld met coordinaten kan worden.
// De array stelt de data voor wanden,vloeren en plafonds voor.
String Windows [];
Windows= new String [lengtewindows];
for (int i=0; i<lengtewindows;i++){
// println(data[i].getChildren("Coordinate"));
Windows[i]= datawindows[i].getContent("Coordinate");
}
//println(Windows);
//saveStrings("windows.txt", Windows);
Answers
Format your code. Select each code and hit ctrl+o. Ensure there is an empty line above and below each code block.
Kf
(i've done it)
can you post the xml file? we can't run the code without it.
alternatively, post a simpler example which shows the problem.
dear koogs and kfrajer, Thank you for having your thoughts on this. the adjusted xml file is found here: https://gist.github.com/dinovandeijzen/86ab650d71fcc4561c7bebeb282403d0 ( i am not sure how to insert a xml file here).
the simpler code is as follows:
The problem is that i thought i wrote the strings correctly to a text file in notepad, though in notepad++ the format shows i failed doing so. Can't figure out how to modify it in the correct format.
I just see that you're saving
Strings with lots of empty spaces before each number
reason:
every CartesianPoint holds 3-4 Coordinates
your wy of doing it gets all 4 Coordinates at once. Hence the ~7 spaces on each number.
Instead try this, there are no spaces anymore.
(instead as putting it into an ArrayList of type String, can't you try PVector or so? )
result:
Dear Chris, I will test your suggestions first thing tomorrow morning. It should lead me in the correct way.
Important is to have the format as :
With your given advice and some fresh energy tomorrow, I am optimistic to Tackle the issue as I think you pointed my biggest flaw.
Chrisir ad koogs,
I managed with your great advice to get the desired output.
I chose to use an Arraylist as I needed to remove the records once written.
I will see if using a Pvector is a better solution, though very happy this step is tackled
output:
CODE:
Hm..
You basically hammer the numbers into the ArrayList without saving where a triple begins or ends
You could make a class for that
Or a 2D array
Hi Chrisir, True, i am looking into it as it seems the way forward for my case.