Populating a three demensional array from a file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • naivewisdom
    New Member
    • May 2010
    • 2

    #1

    Populating a three demensional array from a file

    For a lengthy program which would be in no way helpful to discribe, I need to populate a three dementional array from a file and overwrite the file with the modefied arraylist later. The three dementional arraylist will always be in the form Integer[4][6][8] and the file should have one number on each line which corresponds to a place in the array. For some reason my code is not populating the array at all. Here is the portion of the code which populates the array:
    Code:
    public Integer[][][] populateLearning() throws FileNotFoundException 
    	{
    		Integer[][][] learning2= new Integer[3][5][7];
    		int pile=1;
    		int stoneNumber=0;
    		File f = new File("learningFile.txt");
    		FileReader fr = new FileReader(f);
    		BufferedReader br = new BufferedReader(fr);
    		String eachLine= "waiting to be filled";
    		int a;
    		int b; 
    		int c;
    		while (eachLine != null) 
    		{
    			for( a=0; a<3; a++)
    			{
    				for(b=0; b<5; b++)
    				{
    					for(c=0; c<7; c++)
    					{
    						try {
    							eachLine = br.readLine();
    						} catch (IOException e) {
    							// TODO Auto-generated catch block
    							e.printStackTrace();
    						}
    						learning2[a][b][c]=Integer.parseInt(eachLine);
    						
    					}
    					c=0;
    				}
    				b=0;
    			}
    			 //Prints out each line in file
    		}
    		return learning2;
    	}



    Here is my code for refilling the array:


    Code:
    public void learn(Integer winner)
    	{
    		int adder=0;
    		if(winner==0)
    		{
    			adder=1;
    		}
    		if(winner==0)
    		{
    			adder=-1;
    		}
    		for(int i=1; i<stones.size(); i=i+2)
    		{
    			Integer[] compsMove= stones.get(i);
    			int a= compsMove[0];
    			int b= compsMove[1];
    			int c= compsMove[2];
    			learning[a][b][c]=learning[a][b][c]+adder;
    		}
    		for(int i=2; i<stones.size(); i=i+2)
    		{
    			Integer[] compsMove= stones.get(i);
    			int a= compsMove[0];
    			int b= compsMove[1];
    			int c= compsMove[2];
    			learning[a][b][c]=learning[a][b][c]-adder;
    		}
    		overWriteLearning();
    	}
    	
    	public void overWriteLearning()
    	{	
    		FileOutputStream fout;		
    		try
    		{
    		    // Open an output stream
    		    fout = new FileOutputStream ("learningFile");
    		    // Print a line of text
    		    int a;
    			int b; 
    			int c;
    		
    				for( a=0; a<3; a++)
    				{
    					for(b=0; b<5; b++)
    					{
    						for(c=0; c<7; c++)
    						{
    							new PrintStream(fout).println (learning[a][b][c]);
    						}
    						c=0;
    					}
    					b=0;
    				}
    				 //Prints out each line in file
    		    // Close our output stream
    		    fout.close();		
    		}
    		// Catches any error conditions
    		catch (IOException e)
    		{
    			System.err.println ("Unable to write to file");
    			System.exit(-1);
    		}
    	}

    Any suggestions on how I can get these to work?
    Last edited by RedSon; May 19 '10, 07:50 PM. Reason: Next time add some [CODE] tags!
Working...