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
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?
.. 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
}
}
Bookmarks