Results 1 to 4 of 4
  1. #1
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default Catching Exceptions (quick question)

    So I just wrote this spellchecker and had a couple questions. In the finally block, I cannot close the file in.close() as "in" is initialized in the try block. How do I overcome that? 2nd question, I want the system to exit when the exception is thrown but seems like it system goes on with the program......just doesn't write to hashset and comeback with FALSE.

    Java Code:
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.util.*;
    
    public class SpellChecker {
    
    Set<String> dictionary = new HashSet<String>();
    String fileName = "english";	
    public SpellChecker() {
    try {	
    FileReader reader = new FileReader(fileName);
    Scanner in = new Scanner(reader);
    while(in.hasNextLine()) {
    	dictionary.add(in.nextLine());
    }
    }
    catch (FileNotFoundException exception) {
    System.out.println("Sorry, Could not load the dictionary file named" + fileName);	
    }
    finally {
    	in.close();
    }
    }
    boolean isWord(String word) {
    	if(dictionary.contains(word))
    	return true;
    	else return false;
    }
    
    public static void main(String[] args) {
    
    SpellChecker spell = new SpellChecker();
    System.out.println(spell.isWord("tea"));
    System.out.println(spell.isWord("stone"));
    System.out.println(spell.isWord("cup"));
    System.out.println(spell.isWord("house"));
    System.out.println(spell.dictionary.size());
    }
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,801
    Rep Power
    25

    Default Re: Catching Exceptions (quick question)

    "in" is initialized in the try block
    I assume you mean that in is defined in the try block.
    You need to define in at a higher scope so any code that needs access to it can see it. Give it an initial value of null.
    You can assign it values or use it anywhere it is in scope.

  3. #3
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default Re: Catching Exceptions (quick question)

    Cool tks. that works, i tried before but set it to NULL so compiler thought that it's not initialized

    can you give me an example how i would add a method public void spellCheck(Reader r) throws IOException { .....Not full example I just don't know how to call the method with parameter Reader r.

    tks.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,801
    Rep Power
    25

    Default Re: Catching Exceptions (quick question)

    public void spellCheck(Reader r) throws IOException {
    /// put the code here that can throw the IOException
    } // end method

    You call the method with an instance of a Reader:
    Reader rdrRef = ....

    spellCheck(rdrRef);

Similar Threads

  1. Replies: 2
    Last Post: 06-11-2011, 02:05 AM
  2. catching exceptions (hierarchy)
    By kailash in forum Java Servlet
    Replies: 2
    Last Post: 02-10-2011, 07:43 AM
  3. Help with Catching Exceptions
    By javaman1 in forum New To Java
    Replies: 4
    Last Post: 09-11-2010, 03:06 AM
  4. Client catching Web Service User Exceptions [Java 6.0.17]
    By Shaitan00 in forum Advanced Java
    Replies: 4
    Last Post: 11-22-2009, 02:09 AM
  5. catching uncaught exceptions using Spring / WebApp
    By taille50 in forum Web Frameworks
    Replies: 1
    Last Post: 10-18-2009, 02:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •