Java Try and Catch IOException Issue

I am trying to use some of the code that I found at the bottom of this page . Here is the code in the class that I created for it:

import java.io.LineNumberReader; import java.io.FileReader; import java.io.IOException; public class LineCounter { public static int countLines(String filename) throws IOException { LineNumberReader reader = new LineNumberReader(new FileReader(filename)); int cnt = 0; String lineRead = ""; while ((lineRead = reader.readLine()) != null) {} cnt = reader.getLineNumber(); reader.close(); return cnt; } } 

My goal is to count the lines of a text file, save that number as an integer, and then use that integer in my main class. In my main class, I tried several different ways to make this happen, but (as a new programmer) I'm missing something. Here is the first thing I tried:

 String sFileName = "MyTextFile.txt"; private int lineCount = LineCounter.countLines(sFileName); 

With this attempt, I get the error "unregistered java.io.IOException exception; it should be detected or declared that it will be thrown." I do not understand why I get this because, as I see it, an exception is declared in my countLines method. I tried using the catch try block right below this last bit of code that I posted, but that didn't work either (I don't think I did it right, though). Here is my attempt to catch the attempt:

 String sFileName = "MyTextFile.txt"; private int lineCount;{ try{ LineCounter.countLines(sFileName); } catch(IOException ex){ System.out.println (ex.toString()); System.out.println("Could not find file " + sFileName); } } 

Please show me the way! Thanks in advance for your help!

+7
java try-catch ioexception
source share
4 answers

The initializer block is similar to any bits of code; it does not “attach” to any field / method preceding it. To assign values ​​to fields, you must explicitly use this field as the lhs of the assignment operator.

 private int lineCount; { try{ lineCount = LineCounter.countLines(sFileName); /*^^^^^^^*/ } catch(IOException ex){ System.out.println (ex.toString()); System.out.println("Could not find file " + sFileName); } } 

In addition, your countLines can be simplified:

  public static int countLines(String filename) throws IOException { LineNumberReader reader = new LineNumberReader(new FileReader(filename)); while (reader.readLine() != null) {} reader.close(); return reader.getLineNumber(); } 

Based on my test, it looks like you can getLineNumber() after close() .

+3
source share

Your countLines(String filename) method countLines(String filename) IOException.

You cannot use it in a member’s ad. You need to perform the operation in the main(String[] args) method.

Your main(String[] args) method will receive an IOException that will be passed to it countLines, and it will need to handle or declare it.

Try just throwing an IOException from the main

 public class MyClass { private int lineCount; public static void main(String[] args) throws IOException { lineCount = LineCounter.countLines(sFileName); } } 

or this, to handle it and wrap it in an unchecked IllegalArgumentException:

 public class MyClass { private int lineCount; private String sFileName = "myfile"; public static void main(String[] args) throws IOException { try { lineCount = LineCounter.countLines(sFileName); } catch (IOException e) { throw new IllegalArgumentException("Unable to load " + sFileName, e); } } } 
+1
source share

The reason you get an IOException is because you do not catch the IOException of your countLines method. You want to do something like this:

 public static void main(String[] args) { int lines = 0; // TODO - Need to get the filename to populate sFileName. Could // come from the command line arguments. try { lines = LineCounter.countLines(sFileName); } catch(IOException ex){ System.out.println (ex.toString()); System.out.println("Could not find file " + sFileName); } if(lines > 0) { // Do rest of program. } } 
+1
source share

You are trying to call a non-static variable in a static method (main).

0
source share

All Articles