0

I'm writing a program that uses IOExceptions and I'm coming across an error that I can't figure out. The code is

Main:

public class IOJ { public static void main(String[] args) { findStuff calc = new findStuff(); calc.findSyl(); //^ Unreported exception IOException, must be caught or declared to be thrown calc.printRes(); } } 

and the actual files that's throwing

import java.io.FileReader; import java.io.FileWriter; import java.io.BufferedReader; import java.io.PrintWriter; import java.io.IOException; import java.util.StringTokenizer; public class findStuff { private double findSyl; //find Syl private double printRes; //print results public double NumSylG = 0; //number of syllables public double findSyl()throws IOException { BufferedReader inputStream = null; PrintWriter outputStream = null; try { String newLine = ""; outputStream.println(newLine); String[] tokens = newLine.split(" "); char temp; for (int i = 0; i < newLine.length(); i++) { temp = newLine.charAt( i ); if (Character.isLetter(temp) ) NumSylG++; } } catch (IOException e) { System.err.println("Caught IOException: " + e.getMessage()); } finally { if (inputStream != null) inputStream.close(); if (outputStream != null) outputStream.close(); } return findSyl; } public double printRes() { System.out.printf("The total number of Syl in Gettysburg is: %.1f \n", NumSylG); return printRes; } } 

The findStuff file compiles just fine. it's when I call it from main is when I get that error. I'm still getting used to catching and throwing things so can anyone give me some insight on what I am doing wrong? Note: I didn't put the file path for privacy reasons.

2

1 Answer 1

0
public double findSyl()throws IOException 

findSyl() declares that it throws an IOException, which is a checked exception. This means that the caller of findSyl() must either catch that exception or declare that it may throw that exception too.

Your two options :

1.

 public static void main(String[] args) { findStuff calc = new findStuff(); try { calc.findSyl(); calc.printRes(); } catch (IOException ex) { // handle the exception here } } 

2.

 public static void main(String[] args) throws IOException { findStuff calc = new findStuff(); calc.findSyl(); calc.printRes(); } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.