Results 1 to 7 of 7
  1. #1
    Marisabel is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Question unreported exception java.lang.Exception

    Hello java users. I am fairly new to java and programming. I am trying to create a program that will read a text file and then store information in an array (this is a requisite) and then print it. I am also required to use methods.

    I get the following error message when calling the methods (during compilation):

    Java Code:
    BetseyColonComp2315Proyecto.java:58: unreported exception java.lang.Exception; must be caught or declared to be thrown
             readFile(nombreArchivo);
                     ^
    BetseyColonComp2315Proyecto.java:59: unreported exception java.lang.Exception; must be caught or declared to be thrown
             crearArreglo(Persona)
    ----------------------------------------------

    This the code that I have so far:

    Java Code:
       import java.util.Scanner;
       import java.io.File;
       import java.io.BufferedReader;
       import java.io.BufferedReader;
       import java.io.FileNotFoundException;
       import java.io.FileReader;
    
    
       public class BetseyColonComp2315Proyecto
       {
          public static void readFile(String nombreArchivo)throws Exception
          {
             
             Scanner input = new Scanner(System.in);
             
             System.out.println("Entre el nombre del archivo: ");
             nombreArchivo = input.next();
          }
          
          public static void crearArreglo (String[] args) throws Exception
          {
          //open the text file for reading with scanner
             Scanner input = new Scanner( new File("Archivo.txt") );
             BufferedReader bufferedReader = null;
          
             int i=0;
             String line = null;
          	
             while ((line = bufferedReader.readLine()) != null)
             {
                i++;
             }
          	
             String Persona[] = new String[i];	
          }
          
          public static void displayPersona(String Persona[])
          {
             int counter=0;
             while (counter < Persona.length)
             {
                System.out.println(Persona[counter]);
                counter++;
             }
          }
          public static void main(String[] args)
          {
          
             Scanner input = new Scanner(System.in);
             String Persona[] = new String[10000];
             String nombreArchivo =" ";
          	
             readFile(nombreArchivo);
             crearArreglo(Persona);
             displayPersona(Persona);
             
          }
       	
       }
    Last edited by Fubarable; 04-30-2011 at 05:36 PM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I'd do what the compiler tells you to do and catch your exceptions. To learn how to do this, please check out this very helpful tutorial: Lesson: Exceptions (The Java™ Tutorials > Essential Classes)

    I've added code tags to your post.

  3. #3
    Marisabel is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    Java Code:
     public static void readFile(String nombreArchivo)throws IOException
          {
             
             Scanner input = new Scanner(System.in);
             try
             {
                System.out.println("Entre el nombre del archivo: ");
                nombreArchivo = input.next();
             }
                catch (FileNotFoundException)
                {
                   System.out.println("El nombre del archivo no existe");
                }
          	
          }
    It is asking me to add an identifier at the catch. I have read the tutorials but I am still lost.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What's the new error? Did you add the try catch at the correct areas? You don't need to catch or try anything in this method. You are only using a scanner.

  5. #5
    Marisabel is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default code and error message

    First, thank you for your reply.

    Java Code:
    import java.util.Scanner;
       import java.io.File;
       import java.io.BufferedReader;
       import java.io.BufferedReader;
       import java.io.FileNotFoundException;
       import java.io.FileReader;
       import java.io.IOException;
    
    
       public class BetseyColonComp2315Proyecto
       {
          public static void readFile(String nombreArchivo)throws IOException
          {
             
             Scanner input = new Scanner(System.in);
             try
             {
                System.out.println("Entre el nombre del archivo: ");
                nombreArchivo = input.next();
             }
                catch (FileNotFoundException)
                {
                   System.out.println("El nombre del archivo no existe");
                }
          	
          }
          
          
          public static void crearArreglo (String[] args) throws IOException
          {
          //open the text file for reading with scanner
             Scanner input = new Scanner( new File("Archivo.txt") );
             BufferedReader bufferedReader = null;
          
             int i=0;
             String line = null;
          	
             while ((line = bufferedReader.readLine()) != null)
             {
                i++;
             }
          	
             String Persona[] = new String[i];	
          }
          
          public static void displayPersona(String Persona[])
          {
             int counter=0;
             while (counter < Persona.length)
             {
                System.out.println(java.util.Arrays.toString(Persona));
             
                counter++;
             }
          }
          public static void main(String[] args)
          {
          
             Scanner input = new Scanner(System.in);
             String Persona[] = new String[10000];
             String nombreArchivo =" ";
          	
             readFile(nombreArchivo);
             crearArreglo(Persona);
             displayPersona(Persona);
             
          }
       	
       }
    The first error message was that I was not catching the exceptions.

    The new error message is:

    Java Code:
    Proyecto.java:26: <identifier> expected
                catch (FileNotFoundException)

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Remove the try catch in the method you added it to. It is unnecessary in that method. A scanner is just getting input from the console, while it can throw exceptions, I don't think you have to worry about it. Catching a FileNotFoundException is not a problem because you don't do anything with files in that method.

    The only method you really need to be trying and catching stuff is the method that uses streams. Your way of getting the file is also incorrect. You should be modifying the passed in argument like that. Instead it should loop like this
    Java Code:
    public static File readFile(){
      prompt for string
      File f = new File(input);
      return f;
    }
    That would be a much better way to handle it.

    Once you want to read the file you want to try to open the streams, and catch ioexceptions.

    When you want to do something and fill an array, string, etc, you shouldn't be passing an argument as you are, instead you should be returning something

    Java Code:
    public static void crearArreglo (String[] args)
    
    should be
    
    public static String[] crearArreglo()
    Last edited by sunde887; 05-01-2011 at 02:09 AM.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Marisabel View Post
    Java Code:
    Proyecto.java:26: <identifier> expected
                catch (FileNotFoundException)
    Your catch block is wrong as the exception you're catching must have a variable. Please look at the examples in the tutorial link that I gave you to see how it is done correctly.

Similar Threads

  1. Unreported Exception? Please help!
    By pinhead in forum New To Java
    Replies: 3
    Last Post: 04-19-2011, 07:26 PM
  2. unreported exception java.io.IOException
    By fluffaykitties in forum New To Java
    Replies: 11
    Last Post: 03-07-2011, 01:59 AM
  3. Replies: 5
    Last Post: 02-14-2011, 11:39 PM
  4. Unreported exception java.sql.SQLException
    By javamula in forum AWT / Swing
    Replies: 4
    Last Post: 09-29-2009, 02:32 PM
  5. Error: unreported exception java.io.IOException; ??
    By jonsamwell in forum New To Java
    Replies: 5
    Last Post: 08-24-2008, 04:11 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
  •