0

I am trying to write a method that prints information into an array. the directions are: Create a second method for WordPath: makeWordArray that takes a String file name as input, and it returns either an array or an ArrayList that stores WordData objects.

First, the method should open the file with new FileReader(file), call the numLines method to get the number of lines in file, and then create an array or an ArrayList of that size.

Next, close the FileReader and reopen the file. This time use BufferedReader br = new BufferedReader(new FileReader(file)). Create a loop to run through the file calling br.readLine(). For each line that you read in from br.readLine(), call the parseWordData on that String to get a WordData and store the WordData object into the appropriate index of the array or ArrayList.

My code is:

public class WordPath {

public static int numLines(Reader reader) {
BufferedReader br = new BufferedReader(reader);
int lines = 0;
try {
  while(br.readLine() != null) {
    lines = lines + 1;
  }

  br.close();
}
catch (IOException ex) {
  System.out.println("You have reached an IOException");
}
return lines;

}

 public WordData[] makeWordArray(String file) {
 try {
  FileReader fr = new FileReader(file);
  int nl = numLines(fr);
  WordData[] newArray = new WordData[nl];
  fr.close();
  BufferedReader br = new BufferedReader(new FileReader(file));
  while(br.readLine() != null) {
    int arrayNum = 0;
    newArray[arrayNum] = WordData.parseWordData(br.readLine());
    arrayNum = arrayNum + 1;
  }
}
catch (IOException ex) {
  System.out.println("You have reached an IOException");
}
catch (FileNotFoundException ex2) {
  System.out.println("You have reached a FileNotFoundexception");
}
return newArray;
}  
}

I am running int a problem where the variable newArray can not be found, I believe because it is in the try statement. Is there any way to reformat this to work?

cmart
  • 91
  • 2
  • 12
  • You are right, move the declaration outside the try – Kevin Apr 28 '13 at 00:16
  • I tried, but the problem with that was that that bit of code was dependent on the code above(fileReader fr...) which also could throw an exception. – cmart Apr 28 '13 at 00:25

1 Answers1

1

Like this:

public WordData[] makeWordArray(String file) {
    WordData[] newArray = null;
    try {
        FileReader fr = new FileReader(file);
        int nl = numLines(fr);
        newArray = new WordData[nl];
        fr.close();
        BufferedReader br = new BufferedReader(new FileReader(file));
        while(br.readLine() != null) {
            int arrayNum = 0;
            newArray[arrayNum] = WordData.parseWordData(br.readLine());
            arrayNum = arrayNum + 1;
        }
    }
    catch (IOException ex) {
        System.out.println("You have reached an IOException");
    }
    catch (FileNotFoundException ex2) {
        System.out.println("You have reached a FileNotFoundexception");
    }
    return newArray;
} 

You need to pull the declaration of the variable outside, but leave the assignment to that variable on the inside of the try.

Kevin
  • 24,871
  • 19
  • 102
  • 158