Results 1 to 4 of 4
  1. #1
    timalloy is offline Member
    Join Date
    Apr 2014
    Posts
    12
    Rep Power
    0

    Default Writing Float Array to file

    Hello everyone,

    I am trying to write a Float Array to a file but it is only writing the last line of the array to a file. I have also attached the entire assignment as a zip file with all required files. Here is the part of the code I am having issues with:

    Java Code:
    for (int i = 0; i < differenceArray.length; i++){
        	  System.out.println ("Date: "+ dateArray[i] + " Opening: " + dataArray[i][2] + " Closing: "+ dataArray[i][5] + " Difference: " + differenceArray[i]);
        	  try(FileWriter writer = new FileWriter("C:\\temp\\" + fileName + ".txt")){
    		      writer.write(Float.toString(differenceArray[i]));
    		      writer.flush();
    		      writer.close();
        	  }
        	  catch (IOException e){
      	    	e.printStackTrace();
      	    }
    Here is the whole code:

    Java Code:
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.IOException;
    
    /**
     * 
     * Analyzes a large volume of historical data from a stock (SPLS) on NASDAQ. This version will read 20 lines of test data.
     * @author Matthew Patten
     * @version April 19, 2014
     *
     */
    
    public class Calculations extends ReadFiles{
      public String []dateArray = new String [splsArray.length];
      public float [][]dataArray = new float[splsArray.length][8];
      public int highestOpenIndex = 0;
      public int highestCloseIndex = 0;
      public int lowestOpenIndex = 0;
      public int lowestCloseIndex = 0;
      public int []lowValue = new int[10];
      public int []highValue = new int[10];
      public float []differenceArray = new float[splsArray.length];
      public float difference;
      public float record;
      public String tempDate;
      public float tempOpen;
      private BufferedWriter writer;
      public String fileName = "differences";
     
      /*
       * Separates date the column from splsArray and moves it to dateArray.
       * 
       * @param createDateArray
       * @return The date column is separated from splsArray
       * @throws ArrayIndexOutOfBoundsException
       */
      public void createDateArray(){
        try{
          for (int i = 1; i < splsArray.length; i++){
            dateArray[i] = splsArray[i][1];
          }
        }
        catch (ArrayIndexOutOfBoundsException e){
        	e.printStackTrace();
        }
      }
       
      /*
       * Takes the rest of the data in splsArray and moves it to dataArray.
       * 
       * @param createDataArray
       * @return The rest of the data in splsArray is moved into dataArray
       * @throws ArrayIndexOutOfBoundsException
       */
      public void createDataArray(){
        try{
          for (int i = 0; i < splsArray.length; i++){
            for (int j = 2; j <= 7; j++){
              if (splsArray[i][j] != null){
                dataArray[i][j] = Float.parseFloat(splsArray[i][j]);
              }
              else{
                dataArray[i][j] = 0;
              }
            }
          }
        }  
        catch (ArrayIndexOutOfBoundsException e){
        	e.printStackTrace();
        }
      }
      
      /*
       * Prints the data in the array to show that the date column has been removed
       * from the rest of the data.
       * 
       * @param printNewArray
       * @return dataArray
       * @throws ArrayIndexOutOfBoundsException
       */
      public void printNewArray(){
    	  System.out.println("Open    High    Low     Close   Volume          Adj Close"); //Prints column titles for Array
    		System.out.println("=========================================================");
    	    try{
    	      for (int i = 1; i < splsArray.length; i++){
    	        //System.out.print(dateArray[i]);
    	        for (int j = 2; j <= 7; j++){
    	          System.out.print (dataArray[i][j] + "\t");
    	        }
    	        System.out.println();
    	      }
    	    }
    	    catch (ArrayIndexOutOfBoundsException e){
    	    	e.printStackTrace();
    	    }
    	  }
      
      /*
       * This method calculates and displays the highest/closing values of the array and displays the results
       * either by the highest opening/closing value or the lowest opening/closing value. 
       * 
       * @param performCalculations
       * @return dateArray highestOpenIndex highestCloseIndex lowestOpenIndex lowestCloseIndex highValue lowValue
       */
      public void performCalculations(){
          for (int i = 0; i < dataArray.length; i++){
            if (dataArray[highestOpenIndex][2] <= dataArray[i][2]){
            	highestOpenIndex = i;
            }
            if (dataArray[highestCloseIndex][5] <= dataArray[i][5] || highestCloseIndex == 0){
              highestCloseIndex = i;
            }
          }
          System.out.println ("\nHighest opening " + dateArray[highestOpenIndex] + ": "  + dataArray[highestOpenIndex][2]);
          System.out.println ("\nHighest closing " + dateArray[highestCloseIndex] + ": " + dataArray[highestCloseIndex][5]);
          
          for(int i = 0; i < dataArray.length; i++){
            if (dataArray[lowestOpenIndex][2] > dataArray[i][2] || dataArray[lowestOpenIndex][2] == 0){
                lowestOpenIndex = i;
              }
          }
          
          for(int i = 0; i < dataArray.length; i++){
            if (dataArray[lowestCloseIndex][5] > dataArray[i][5] || dataArray[lowestCloseIndex][5] == 0){
              lowestCloseIndex = i;
            }
          }
          
          System.out.println ("\nLowest opening " + dateArray[lowestOpenIndex] + ": " + dataArray[lowestOpenIndex][2]);
          System.out.println ("\nLowest closing " + dateArray[lowestCloseIndex] + ": " + dataArray[lowestCloseIndex][5]);
          System.out.println ("\nTop 10 dates with the highest closing:");
          System.out.println("Date         Closing");
          System.out.println("====================");
          
          for (int i = 0; i < dataArray.length ; i++){
        	  for(int j = 0; j < highValue.length; j++)
        	  if(dataArray[i][3] > dataArray[highValue[j]][3]){
        		  
        		  for(int k = highValue.length-1; k > j ; k--){
        			  highValue[k] = highValue[k-1];
        		  }
        		  highValue[j] = i;
        		  break;
        	  }
          }
    	      
          for (int i = 0; i < lowValue.length; i++){
            System.out.println (dateArray[highValue[i]] + " - " + dataArray[highValue[i]][3]);
          }
          System.out.println("\nTop 10 dates with the lowest closing:");
          System.out.println("Date         Closing");
          System.out.println("====================");
          
          for (int i = 0; i < dataArray.length ; i++){
        	  for(int j = 0; j < lowValue.length; j++){
    	    	  if( lowValue[j] == 0){
    	    		  lowValue[j] = i;
    	    		  break;
    	    	  }
    	    	  if(dataArray[lowValue[j]][4] > dataArray[i][4]){
    	    		  for(int k = lowValue.length-1; k > j ; k--){
    	    			  lowValue[k] = lowValue[k-1];
    	    		  }
    	    		  lowValue[j] = i;
    	    		  break;
    	    	  }
        	  }
          }
          for (int i = 0; i < lowValue.length; i++){
            System.out.println (dateArray[lowValue[i]] + " - " + dataArray[lowValue[i]][4]);
          }
          for (int i = 0; i < dataArray.length ; i++){
        	  differenceArray[i] = dataArray[i][2] - dataArray[i][5];  
          }
          System.out.print("\n");
          for (int i = 0; i < differenceArray.length; i++){
        	  System.out.println ("Date: "+ dateArray[i] + " Opening: " + dataArray[i][2] + " Closing: "+ dataArray[i][5] + " Difference: " + differenceArray[i]);
        	  try(FileWriter writer = new FileWriter("C:\\temp\\" + fileName + ".txt")){
    		      writer.write(Float.toString(differenceArray[i]));
    		      writer.flush();
    		      writer.close();
        	  }
        	  catch (IOException e){
      	    	e.printStackTrace();
      	    }
          }
        }
      }
    Attached Files Attached Files
    Last edited by timalloy; 04-20-2014 at 10:08 PM.

  2. #2
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    6

    Default Re: Writing Float Array to file

    The problem is, that FileWriter writer = new FileWriter(filename) is overriding the file by default. So for each element the file is overridden.

    I see 2 possible solutions:
    Put the for loop inside the try() part. That way you override the file once only and write all elements before you close it.
    You could use the constructor that takes filename and boolean value append.

    And by the way: if you use the try (...) syntax, the close call is done automatically so your call of close is not required.

    Konrad

  3. #3
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    311
    Rep Power
    9

    Default Re: Writing Float Array to file

    ...or do your manipulation with array outside try/catch code (use try/catch only to write to file).

  4. #4
    timalloy is offline Member
    Join Date
    Apr 2014
    Posts
    12
    Rep Power
    0

    Default Re: Writing Float Array to file

    Quote Originally Posted by kneitzel View Post
    The problem is, that FileWriter writer = new FileWriter(filename) is overriding the file by default. So for each element the file is overridden.

    I see 2 possible solutions:
    Put the for loop inside the try() part. That way you override the file once only and write all elements before you close it.
    You could use the constructor that takes filename and boolean value append.

    And by the way: if you use the try (...) syntax, the close call is done automatically so your call of close is not required.

    Konrad
    Konrad,

    Putting the for loop inside the try() worked perfectly! Thank you very much.

Similar Threads

  1. Replies: 3
    Last Post: 10-07-2011, 01:39 AM
  2. Replies: 7
    Last Post: 04-26-2011, 04:27 PM
  3. writing string array in to a file
    By abdullahansari in forum New To Java
    Replies: 13
    Last Post: 08-29-2010, 05:25 AM
  4. Writing a countdown array to a file.
    By kewlgeye in forum New To Java
    Replies: 6
    Last Post: 05-25-2008, 07:09 AM
  5. Reading/Writing a File using byte array
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-16-2008, 11:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •