Jon’s PhD Journal

March 11, 2007

Sunday: more coding …

Filed under: Coding — JDE @ 4:57 pm

Back on the case today: trying to work out why I can’t print bird positions to a text file. I’ve moved the code around a little to include a try-catch expression, which compiles okay — the simulation now works. I can get the simulatino to output the results to the screen. However, I can’t get the system to write to a text file for some reason — grr!! This is what happended the last time though … for some reason the code would not dave to a text file, but *something* happened which then enabled it to.

Ahah! Success! I appended a “true” to the code that opens a BufferedWriter, and it now works! Code follows below:

try {
        BufferedWriter out = new BufferedWriter (new FileWriter(“boidposition_out.txt”, true));
        while((b = boidsList.getBoid(i)) != null) {
          b.animateBoid();
          float x = b.getBoidPosX();
          float y = b.getBoidY();
          float z = b.getBoidZ();
                             
          String s_x = Float.toString(x);
          String s_y = Float.toString(y);
          String s_z = Float.toString(z);
         
          String position_result = i + “,” + s_x + “,” + s_y + “,” + s_z + “\n”;
          System.out.print(position_result); // print to screen
         
          out.write(position_result); // print to text file
          i++;
        } // end of while
        out.close();
    } catch (IOException e) {
        System.out.println(“Exception ” + e);
    }

But what happens if I increase the number of birds in the simulation –> 20 (currently have 10) …? I get 20 locations outputted in the text file!!

However, there is an issue: I can’t print the timestep for some reason: i.e. just a counter that increments for each cycle of position changes. Hmm mmore thought is required …

Blog at WordPress.com.