Note that there are some explanatory texts on larger screens.

plurals
  1. POTry and catch scope problems
    primarykey
    data
    text
    <p>I am having trouble trying to figure out what is suppose to be placed in what scope when handling with try and catch, I have part of my code asks a user to type a file name they want scanned, this is the try and catch statements:</p> <pre><code>try{ System.out.println("Enter the name of the file you wouldlike to scan: "); String fileName = scan.nextLine(); File file = new File(fileName); BufferedReader br = new BufferedReader(new FileReader(fileName) } catch( IOException ioe){ System.out.println(ioe); System.exit(-1); } </code></pre> <p>When i compile it cannot find the symbol "br" in "line = br.readLine();" //part of the code that scans the file, I am not sure what to place in which scope in the try statement</p> <p>here is the another(not the whole program) part of the code, i am tested this part with system.in and works fine, not working with filereader though</p> <pre><code>String line; int lineCount = 0, wordCount = 0, charCount = 0, palCount = 0; int thisLineWords = 0, thisLineChars = 0; boolean isPal = true; do { try { line = br.readLine(); } catch( Exception e ) { break; } if( line == null ) break; if( line.compareTo(".") == 0 ) break; thisLineWords = 0; thisLineChars = line.length() + 1; // count chars isPal = true; //count words boolean inWord = false; for( int i = 0; i &lt; line.length(); i++ ) { char ch = line.charAt(i); if( Character.isWhitespace(ch) ) { if( inWord ) inWord = false; } else { if( !inWord ) { inWord = true; thisLineWords++; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload