Hello Guest

[SOLVED]trouble with writing/reading array to/from file

  • 1 Replies
  • 3434 Views
[SOLVED]trouble with writing/reading array to/from file
« on: March 03, 2012, 22:37:52 »
I'm using a mid-point displacement algorithm to generate a map, and the height values are then stored in an array.
The trouble I'm having is loading the numbers back out of the file and into the array when i press F7.

Here's my code for the reader/writer object:
Code: [Select]
public class Writer {
    
    final int DATA_SIZE = 257;

        FileWriter fw;
        Scanner sc;
        File filename = new File("savedmap.grid");
    
    
    public void write(MPDGenerator mpd) throws IOException{
        
        fw = new FileWriter(filename);
          
        
           fw.write("version=1.0 seed="+mpd.RSEED+"\n");
        for(int j=0;j<DATA_SIZE-1;j++){
            for(int k=0;k<DATA_SIZE-1;k++){
                
                fw.write((int)(mpd.data[j][k]+2000)+"/");
                
            }
            fw.write("\n");
        }
        
        
        
        
    }
    
    public void read(MPDGenerator mpd) throws FileNotFoundException {
        sc = new Scanner(filename).useDelimiter("/");
        sc.nextLine();
        
        for(int j=0;j<DATA_SIZE-1;j++){
            for(int k=0;k<DATA_SIZE-1;k++){
              
                if (sc.hasNextInt()) {
                    mpd.data[j][k] = (sc.nextInt()-2000);
                }
            }
        }
        mpd.print();
    }
    
}

It doesn't give me any Errors, but doesn't reload the map.

mpd.print(); is the method in the generator which converts the height values into a texture array for the renderer(which uses lwjgl)

the +2000 and  -2000 is so it looks neater in the file.
« Last Edit: March 14, 2012, 03:12:04 by MaximusPrime »

Re: trouble with writing/reading array to/from file
« Reply #1 on: March 06, 2012, 14:15:58 »
Hmm, is the action behind your F7 key ever executed? Actually we only see the save/write logic. Have you testet if the load function is ever called?