Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: try-catch block reports to have an Error that I don't know how to fix

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation try-catch block reports to have an Error that I don't know how to fix

    // This program opens a text file and reads the contents of the text
    // file, name of which is specified as a command-line argument
      import java.io.*;
     
      public class ReadFile
      {
     
    	  public static void main(String[] args) throws IOException
    	  {
    		  int i;
    		  FileInputStream theFile;
     
    		  try
    		  {
    			  theFile = new FileInputStream(args[0]);
     
     
    		  }
     
    		  catch(FileNotFoundException exc)
    		  {
    			  System.out.println("The file you requested was not found, please doublecheck the name");
     
    		  }
     
     
    		  //catches the ArrayIndexOutOfBounds for the commandline arguments when a user
    		  //forgets to enter the name of the file as the command line arguments
     
    		  catch(ArrayIndexOutOfBoundsException e)
    		  {
    			  System.out.println("Abide by the syntax\n"+
    			                      "Usage: java ReadFile fileName");
     
              }
     
             // read bytes (the file ) until EOF is reached
     
             do
             {
    			 i = theFile.read(); // reads from the file
     
    			 if(i!= -1)
    			 System.out.print((char)i);
     
    		 } while(i != -1);
     
    		 theFile.close();
     
    	 } // ends main
     
     } // ends class ReadFile
    Last edited by Freaky Chris; June 24th, 2011 at 03:32 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: try-catch block reports to have an Error that I don't know how to fix

    This program opens a text file and reads the contents of the text file, name of which is specified as a command-line argument
    Ok then what happens?

Similar Threads

  1. how do i try/catch this?
    By safra in forum Exceptions
    Replies: 6
    Last Post: October 29th, 2011, 11:14 AM
  2. Java sliding block puzzle issues
    By Vesper in forum What's Wrong With My Code?
    Replies: 15
    Last Post: April 11th, 2011, 07:30 AM
  3. logic error somewhere.. working with try & catch, simple array.. please help
    By basketball8533 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 9th, 2010, 12:40 PM
  4. Creating Block cipher without the use of inbuilt java funcs
    By fortune2k in forum Algorithms & Recursion
    Replies: 0
    Last Post: November 15th, 2010, 09:43 AM
  5. catch all
    By silverspoon34 in forum Exceptions
    Replies: 1
    Last Post: November 29th, 2009, 02:18 PM