-3

The program reads from a data file, pulls out substrings and places them into a string array. The reader is closed. The data is then reduced and written to an output file. Then the writer is closed. In both of those cases, br.close and writer.close, the compiler returns cannot find symbo referring to the br and writer variables. Noted that the scan.close() compiles. i don't understand what is wrong here.

import java.util.Scanner;
import java.io.*;
import java.text.NumberFormat;

public class Module10A_Revision1{

   public static void main ( String[] args )throws FileNotFoundException{


   String dataPoint;
   String[][] data;
   String[][] compressedData = new String[56][5];
   int totalDataPoints = 0;
   int count = 0;

   int state = 0;
   int total = 0;
   int children = 0;
   int poverty = 0;
   int tempState    = 0;
   int tempTotal      = 0;
   int tempChildren = 0;
   int tempPoverty  = 0;

//MEASURE SIZE FOR ARRAY

      File file = new File("data.txt");
      Scanner scan = new Scanner(file);

      //Determine the total number of items listed in the file

      while (scan.hasNextLine()){

         scan.nextLine();
         totalDataPoints++;
      }
      scan.close(); (COMPILER HAS NO ISSUES WITH THIS ONE. BUT 2 BELOW FAIL.)

      data = new String[totalDataPoints][6];

//READ

      try{

         FileInputStream fstream = new FileInputStream("data.txt");
         BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); 


         //equate a substring to a temp variable
         while ((dataPoint = br.readLine()) != null){

            String stateCode = dataPoint.substring(1,2);
            String districtID = dataPoint.substring(4,8);
            String districtName = dataPoint.substring(10,81);
            String totalPop = dataPoint.substring(83,90);
            String childrenPop = dataPoint.substring(92,99);
            String povertyPop = dataPoint.substring(101,108);

            //read into array         

            data[count][0] = stateCode;
            data[count][1] = districtID;
            data[count][2] = districtName;
            data[count][3] = totalPop;
            data[count][4] = childrenPop;
            data[count][5] = povertyPop;

            count++;
         }//end while 

      }//end try
      catch (Exception e){
         e.printStackTrace();
      }//end catch
      br.close(); 

(br is one of the two variables the compiler cannot find)

//PROCESS DATA

      for (int j = 0; j < 56; j++){

      state = 0;
      total = 0;
      children = 0;
      poverty = 0;      

         for (int i = 0; i < totalDataPoints; i++){

            tempState    = Integer.parseInt(data[i][0]);
            tempTotal    = Integer.parseInt(data[i][3]);
            tempChildren = Integer.parseInt(data[i][4]);
            tempPoverty  = Integer.parseInt(data[i][5]);

            if (i == tempState){

               total = total + tempTotal;
               children = children + tempChildren;
               poverty = poverty + tempPoverty;

            }
            else{
               break;
            } // end if

         }//end for j

         NumberFormat defaultFormat = NumberFormat.getPercentInstance();
          defaultFormat.setMinimumFractionDigits(2);
         double perCent = 100*poverty/children;
         //perCent = defaultFormat.format(perCent);

         compressedData[j][0] = Integer.toString(tempState);
         compressedData[j][1] = Integer.toString(total);
         compressedData[j][2] = Integer.toString(children);
         compressedData[j][3] = Integer.toString(poverty);
         compressedData[j][4] = Double.toString(perCent);

      }//end for i


      //create buffer for writing string
      //BufferedWriter writer = new BufferedWriter(new FileWriter("compressedData.txt"));

//WRITE

      try{

         FileWriter writer = new FileWriter("compressedData.txt");  

         // Loop over the elements in the string array and write each line.
         for(int i = 0; i < 56; i++){

            for(int j = 0; j < 5; j++){

               writer.append(compressedData[i][j]);

               if(j < compressedData[j].length - 1)  
                  writer.append(',');  
               else 
                  writer.append('\n');

            }// end j

         }//end i

      }//end try
      catch(IOException e){  

         e.printStackTrace();  

      } //end catch
      finally{

         writer.close(); ( writer IS THE OTHER VARIABLE THE COMPILER CANNOT FIND)

      }//end finally

   }//end main

}//end class
John Joe
  • 12,412
  • 16
  • 70
  • 135
Paul
  • 9
  • 1

1 Answers1

0

THE COMPILER RETURNS "CANNOT FIND SYMBOL" REFERRING TO THE br AND writer VARIABLES.

The problem is that you declared the br inside the try block, but use it outside try-catch block.

You should declare br as global or declare in the main method

BufferedReader br ;

Then only br= new BufferedReader(new InputStreamReader(fstream));

John Joe
  • 12,412
  • 16
  • 70
  • 135