Results 1 to 7 of 7
- 04-30-2011, 06:27 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
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 06:36 PM. Reason: code tags added
-
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.
- 05-01-2011, 02:34 AM #3
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
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"); } }
- 05-01-2011, 02:37 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 15
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.
- 05-01-2011, 02:55 AM #5
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
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 new error message is:
Java Code:Proyecto.java:26: <identifier> expected catch (FileNotFoundException)
- 05-01-2011, 03:05 AM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 15
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; }
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 03:09 AM.
-
Similar Threads
-
Unreported Exception? Please help!
By pinhead in forum New To JavaReplies: 3Last Post: 04-19-2011, 08:26 PM -
unreported exception java.io.IOException
By fluffaykitties in forum New To JavaReplies: 11Last Post: 03-07-2011, 02:59 AM -
unreported exception java.lang.Exception; must be caught or declared to be thrown
By arc_remiel in forum New To JavaReplies: 5Last Post: 02-15-2011, 12:39 AM -
Unreported exception java.sql.SQLException
By javamula in forum AWT / SwingReplies: 4Last Post: 09-29-2009, 03:32 PM -
Error: unreported exception java.io.IOException; ??
By jonsamwell in forum New To JavaReplies: 5Last Post: 08-24-2008, 05:11 AM
Bookmarks