DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2005
    Posts
    22

    Cannot resolve symbol variable

    I'm doing something stupid again and i'm not seeing it. Ive attached the whole program and a text file in case anyone wants to try it but here's my problem:
    all works fine until I try to call 'calculate' from main. I'm getting the 'cannot resolve symbol variable' and I don't understand why. This all worked when I tried it in a program by itself.

    In the main method i have: calculate(dataArray);
    To call:
    public static void calculate( int dataArray[]){
    int[] tempHolding = new int [dataArray.length];
    calculate(dataArray, tempHolding, 0); // call private calculate function and pass perameters
    }

    private static void calculate(int dataArray[], int tempHolding[], int last){
    for (int i = last; i < dataArray.length; i++){
    tempHolding[i] = dataArray[i];
    calculate(dataArray, tempHolding, i +1);
    tempHolding[i] = 0;
    }
    for (int i = 0; i < tempHolding.length; i++){
    if (tempHolding[i] != 0){
    System.out.print (tempHolding[i] + " ");
    }
    System.out.println();
    } //END OF RECURSIVE ADDITION
    Attached Files

  2. #2
    Join Date
    Aug 2005
    Posts
    17
    in main, are you sure dataArray is defined properly? Where are you creating the dataArray array?

    For more help, www.NeedProgrammingHelp.com

  3. #3
    Join Date
    Feb 2005
    Posts
    22
    I was calling 'calculate' after closing the buffer - it I call it before I close the buffer the program runs fine. That's what I don't understand - if i've taken the data and read it into an array, why can't I close the I/O buffer?

  4. #4
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    It compiles and runs now..

    .. but thats all.
    I've added a FileDialog and declared dataArray
    where it is accessible for the rest of the code.

    I also got rid of the static stuff; static should be used when its useful,
    and the main() should only be used for creating the first (GUI) class
    and invoking it.

    Code:
    /**
     * Module  - Recursion Lab
     *
     *
     */
    
    import java.io.*;
    import java.lang.*;
    import java.util.*;
    import javax.swing.*;
    import java.awt.*;
    
    public class RecursionLab {
    
      public static void main(String[] args) {
        RecursionLab rL=new RecursionLab();
        rL.doTheJob();
      }
      public void doTheJob() {
        int dataArray[]=null;
        /* ***Get the input file name from the user and try to create the
         * input stream.  Print a message and terminate the program if a
         * FileNotFoundException error occurs 					*** */
        FileDialog fd=new FileDialog(new Frame(),"Select File",FileDialog.LOAD);
        fd.setVisible(true);
        String fileName=fd.getFile();
        if (fileName==null) {
          return; // user cancelled
        }
    
        String inputFileName = fd.getDirectory()+fileName;
    
        // a try block is used to catch exceptions, should one occur
        try {
    
          // create a File object based on user input
          File f = new File(inputFileName);
    
          // create a BufferedReader for the file
          BufferedReader bfr = new BufferedReader(new FileReader(f));
    
          String text;
          System.out.println(inputFileName + "\n\n");
    
          /* ***  read the first line of the test file which should tell us the
           * number of boxes to use in our calculations - Used to create an array
           * to hold the remainder of the data	*/
    
          int fSize = Integer.parseInt(bfr.readLine());
    
          // create array based on the first number written in the text file
          dataArray = new int[fSize];
          int arrayCounter = 0;
          int boxNumber = 1;
    
          // read the rest of the file in as test data
          while ( (text = bfr.readLine()) != null) {
            //int intLine = Integer.parseInt(text.trim());
            dataArray[arrayCounter] = Integer.parseInt(text.trim());
    
            System.out.println("Box " + boxNumber + ":  " + dataArray[arrayCounter]); // Tempororay
            arrayCounter++;
            boxNumber++;
          }
          System.out.println("Number of Boxes: " + fSize + "\n"); /////Move to end of box list
    
          // close the file when done - ints are now in the array
          bfr.close();
        }
        catch (IOException ioe) {
          // an IOException is thrown when there is some problem accessing a file
          System.out.println("Error reading data file.\n");
          ioe.printStackTrace();
        }
        catch (NumberFormatException nfe) {
          // a NumberFormatException is thrown when an attempt is made to convert
          // a non-integer string to an integer
          System.out.println("Error - non-integer value found.\n");
          nfe.printStackTrace();
        }
        catch (Exception e) {
          // generic catch block - this handles any type of Exception
          System.out.println("Error.\n");
          e.printStackTrace();
        }
        calculate(dataArray);
        System.out.println("\nYou have reached the end of the program.\n"); // Temporary
        System.exit(0);
      }
    
      /* CODE HERE DOES THE RECURSIVE ADDITION */
      //Don't forgetto do calculations
      public void calculate(int dataArray[]) {
        int[] tempHolding = new int[dataArray.length];
        calculate(dataArray, tempHolding, 0); // call private calculate function and pass perameters
      }
    
      private void calculate(int dataArray[], int tempHolding[], int last) {
        for (int i = last; i < dataArray.length; i++) {
          tempHolding[i] = dataArray[i];
          calculate(dataArray, tempHolding, i + 1);
          tempHolding[i] = 0;
        }
        for (int i = 0; i < tempHolding.length; i++) {
          if (tempHolding[i] != 0) {
            System.out.print(tempHolding[i] + " ");
          }
          System.out.println();
        } //END OF RECURSIVE ADDITION
        // prove that it read the file
    
      }
    
    }
    eschew obfuscation

Similar Threads

  1. Help with class/applet
    By none_none in forum Java
    Replies: 17
    Last Post: 04-28-2005, 03:00 PM
  2. Replies: 5
    Last Post: 10-17-2002, 01:58 PM
  3. cannot resolve symbol
    By Janie in forum Java
    Replies: 0
    Last Post: 09-08-2001, 11:16 AM
  4. Cannot Resolve Symbol Error
    By jay in forum Java
    Replies: 2
    Last Post: 05-17-2001, 11:15 AM
  5. compiling
    By John Butorac in forum Java
    Replies: 2
    Last Post: 12-16-2000, 12:27 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links