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

    Default trouble with JFileChooser

    I am working on my assignment, where we have to read data from a file and do some searching and replacing, and write back to the file. We have to get the file object using JFileChooser and I keep getting an error. I wrote a simple little app to test it, and Im getting the same error on the Scanner line:
    Java Code:
    unreported exception java.io.FileNotFoundException: must be caught or declared to be thrown
    Java Code:
    import java.util.*;
    import java.io.*;
    import javax.swing.JFileChooser;
    
    
    public class GeneFinder {
        public static void main(String[] args)
        {
          JFileChooser fileChooser = new JFileChooser();
          if (fileChooser.showOpenDialog(null)== JFileChooser.APPROVE_OPTION)
          {
            File file = fileChooser.getSelectedFile();
            Scanner input = new Scanner(file);
            
            while(input.hasNext())
               {
            System.out.println(input.nextLine());
               }
          input.close();
          }
        }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default

    You need to read up on exceptions and try{}catch blocks.

    A method you are using is declared to throw an exception. Your code must react to this some how in case the exception is thrown by the method.
    Normally this is done by enclosing the method in a try{} catch block.

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

    Default

    Well, I looked up throws Exception, and added it to my main class, and it worked. Thanks a lot Norm.
    Last edited by grcunning; 09-04-2010 at 08:03 PM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default

    That's the second why of reacting to an exception, pass it to the caller of the current method. Its better to handle it locally if you can. For your simple starter program that should be ok. Later you WILL have to know how to catch exceptions when you are dealing with user input since you can't keep the user from entering bad data.

Similar Threads

  1. jfilechooser
    By ranadav in forum AWT / Swing
    Replies: 7
    Last Post: 06-03-2010, 02:44 PM
  2. Jfilechooser
    By greatmajestics in forum Java 2D
    Replies: 5
    Last Post: 03-30-2010, 06:13 PM
  3. help with JFileChooser
    By morgatron in forum New To Java
    Replies: 1
    Last Post: 01-08-2010, 09:00 AM
  4. JfileChooser Help
    By simmi in forum AWT / Swing
    Replies: 9
    Last Post: 08-04-2009, 02:24 AM
  5. using jfilechooser
    By jagadhguru in forum AWT / Swing
    Replies: 3
    Last Post: 10-30-2008, 12:17 PM

Posting Permissions

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