-1

I'm trying to write a class that handles reading files. In order to read the file word by word, I used the following code which I found on the internet. Netbeans seems to disagree and says that it cannot find the symbole br inside the while loop.

public class Reader {
  public String file2string() {
    String line;
    try (InputStream fis = new FileInputStream("smth")) {
      InputStreamReader isr = new InputStreamReader(fis, Charset.forName("UTF-8"));
      BufferedReader br = new BufferedReader(isr);
    } catch (IOException ex) {
      Logger.getLogger(Reader.class.getName()).log(Level.SEVERE, null, ex);
    }
    {
      while ((line = br.readLine()) != null) {
        String[] words = line.split(" ");
        // Now you have a String array containing each word in the current line
      }
    }
    return line;
  }
}
Paradox
  • 4,602
  • 12
  • 44
  • 88
Majd
  • 3
  • 3

3 Answers3

1

You have a {} block after your try statement

{
    InputStreamReader isr = new InputStreamReader(fis, Charset.forName("UTF-8"));
    BufferedReader br = new BufferedReader(isr);
}

And then you have another {} block.

{
    while ((line = br.readLine()) != null) {
        String[] words = line.split(" ");
        // Now you have a String array containing each word in the current line
    }
}

Variables declared in the first block are not visible in the second block.

Combine the two blocks:

public String file2string() {
    String line;
    try (InputStream fis = new FileInputStream("smth")) {
        InputStreamReader isr = new InputStreamReader(fis, Charset.forName("UTF-8"));
        BufferedReader br = new BufferedReader(isr);
        while ((line = br.readLine()) != null) {
            String[] words = line.split(" ");           
        }
        return line;
    } catch (IOException ex) {
        Logger.getLogger(Reader.class.getName()).log(Level.SEVERE, null, ex);
    }
    // you need to decide what you want to return here if you got an exception.
}

You seem to be splitting each line and ignoring the result, and then returning the last line of the file. I don't know why you're doing that, or what you were actually trying to do; but this is how to fix the compile error.

khelwood
  • 55,782
  • 14
  • 81
  • 108
1

The variable "br" is declared in the try{} block, so that's its scope. It won't be visible outside of that block. Try putting the while loop inside the try block.

Graeme Cole
  • 276
  • 1
  • 4
1

Your loop is out the try so the variable br is unknown in the context. Put your while-loop inside the try structure like here:

public String file2string() {
    String line = null;
    try (InputStream fis = new FileInputStream("smth")) {
        InputStreamReader isr = new InputStreamReader(fis, Charset.forName("UTF-8"));
        BufferedReader br = new BufferedReader(isr);

        while ((line = br.readLine()) != null) {
            String[] words = line.split(" ");
            // Now you have a String array containing each word in the current line
        }
    } catch (IOException ex) {
        Logger.getLogger(Reader.class.getName()).log(Level.SEVERE, null, ex);
    }  
    return line;
}
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183