Writing strings are failing

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.

data

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);
Tagged:

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.

  • edited January 2017

    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:

    XML xml;
    ArrayList<String> numbers;
    
    
    void setup() {
      xml = loadXML("OA_4.xml");
      PrintWriter output = createWriter("hoppa.txt");
      numbers = new ArrayList<String>();
    
    
      ///////////////////////////////////////////////////
      /////retrieve names
      //////////////////////////////////////////////////
    
          XML[] namesolids= xml.getChildren("Campus/Surface/CADObjectId");
          for (int i=0; i<namesolids.length; i++) {
            // println(namesolids[i].getChildren());
          }
    
    
        //////////////////////////////////////////////////
        /////retrieve coordinates ( x,y,z times 4 (12 coordinates) to form a plane)
        //////////////////////////////////////////////////
    
      XML[] datasolids= xml.getChildren("Campus/Surface/PlanarGeometry/PolyLoop/CartesianPoint");
      int lengtesolids=datasolids.length;
    
      for (int i=0; i<lengtesolids; i++) {
        numbers.add( new String(datasolids[i].getContent("Coordinate")));
      }
    
    
        //////////////////////////////
        /// print the data into a text file)
        //////////////////////////
    
    
      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
    }
    

    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.

  • edited January 2017 Answer ✓

    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? )

    XML xml; 
    ArrayList<String> numbers;
    
    void setup() { 
    
      size (600, 600);
    
      xml = loadXML("OA.xml"); 
      PrintWriter output = createWriter("hoppa.txt"); 
      numbers = new ArrayList();
    
      /////////////////////////////////////////////////// /////retrieve names ////////////////////////////////////////////////// 
      XML[] namesolids= xml.getChildren("Campus/Surface/CADObjectId"); 
    
      for (int i=0; i<namesolids.length; i++) { 
        // 
        println(namesolids[i].getChildren());
      }
    
      ////////////////////////////////////////////////// /////retrieve coordinates ( x,y,z times 4 (12 coordinates) to form a plane) //////////////////////////////////////////////////
    
      XML[] datasolids= xml.getChildren("Campus/Surface/PlanarGeometry/PolyLoop/CartesianPoint"); 
      int lengtesolids=datasolids.length;
    
      for (int i=0; i<lengtesolids; i++) {
        XML[] animals = datasolids[i].getChildren("Coordinate");
        for (int i2=0; i2<animals.length; i2++) {
          println(trim(animals[i2].getContent("Coordinate")));
        }
      }
      //
    }
    //
    

    result:

    Basic Wall: Generic - 200mm [309147]
    Basic Wall: Generic - 200mm [309148]
    Basic Wall: Generic - 200mm [309149]
    Basic Wall: Generic - 200mm [309150]
    Compound Ceiling: Plain [309173]
    Floor: Generic 150mm [309163]
    -23.0401
    17.4391
    0
    -23.0401
    17.4391
    8
    16.9599
    17.4391
    8
    16.9599
    
  • edited January 2017

    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 :

     Basic wall: Generic - 200mm [309147]
     0
     0
     12
         -23.0401  17.4391. 0
         -23.0401. 17.4391. 8
         16.9599. 17.4391. 8
         16.9599. 17.4391. 0
    
    basic wall: Generic [309148]
     0
     0
     12
       23.  4.  0
       16.7  12  8
       14.2   13  0
       12  0  8
    Etcetera
    

    With your given advice and some fresh energy tomorrow, I am optimistic to Tackle the issue as I think you pointed my biggest flaw.

  • edited January 2017

    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:

    data

    CODE:

    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++){
      XML[] pregather = datasolids[i].getChildren("Coordinate");
          for (int i2=0; i2<pregather.length; i2++){
            numbers.add((pregather[i2].getContent(("Coordinate"))));
            //println( numbers.get(i2));
    }
    
    }
    
    
    
    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(0)+"  " + numbers.get(1) + "  " +         numbers.get(2));
                //println(numbers.get(j));
                numbers.remove(0);
                numbers.remove(0);
                numbers.remove(0);
                //numbers.remove(0);
             }
    
  • 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.

Sign In or Register to comment.