Handling an exception

Hello experts. I need some urgently!! this is the problem:

design and implement a program that creates an exception class called StringTooLongException designed to be thrown wheb a string is discovered that has too many characters in it.
In the main driver of the program, ask the user for a file name. The file is expected to contain data values(strings). What can go wrong? There are two principal concerns: The file may not exist, and the file may have data in the wrong format. Handle the FileNotFoundException exception by printing an appropriate message and allowinf the user to re-enter a new file name as many times as necessary.don't be concerned about the data being in the wrong format .

Then, read strings from the file until the file end. If a string is read that has too manu characters(say 20) ,throw the StringTooLongException exception. Handle this  exception by printing an appropriate, and then continues processing more strings. after processing the entire  file , print "task completed" on the console screen.


This is what I have:
import java.util.*;
import java.io.*;

public class TestProgStringTooLongException
{
   static Scanner console = new Scanner(System.in);
   static final int maxStringSize = 20;

   public static void main(String[] args)
   {
     String filename;
     boolean done;
     boolean eof;
     String str;
     int StringLength;

     done = false;
     eof = false;

     do
     {
       try
       {

               System.out.println(" Enter a file name :");
               filename = console.next();
               System.out.println();
               done = true;
           Scanner inFile = new Scanner(new FileReader("J:\\InputString.txt"));
               System.out.println( " The file name is : " + filename);


       }
       catch( FileNotFoundException fnfRef)
       {
               str = console.next();
               System.out.println(" Exception " + fnfRef.toString()+ " "+ str);
         }


     }while(!done);




   }
  I already have  a series of string store in a file called J:\\InputString.txt
I don't know how to make the user put a file name and handle the file not found exception and how to make the user puta file as many as possible.
thank you.
yamazoAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

avinthmCommented:
First of all - to throw StringTooLongException, you need to create a class StringTooLongException which extends Exception class
Mick BarryJava DeveloperCommented:
take the string entered by the user and create a File

File f = new File(filename);

following shows u how to read lines from file

http://javaalmanac.com/egs/java.io/ReadLinesFromFile.html
CEHJCommented:
You need to make sure the filename is long enough. You need to open the file with the name the user chooses
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

WebstormCommented:
after extending the Exception class, you can add a catch block for your new exception class :

       catch( StringTooLongException too_long_ex)
       {
       }

then you can throw it when you encounter a string whose length is too loog :

      if (line.length()>LIMIT) throw new StringTooLongException
               ("String too long (max="+LIMIT+", found="+line.length()+")");
yamazoAuthor Commented:
thank you experts.But .I'm stuck on the first step which is to ask the user for an a file name.  I use the code in the link that Objects you provide , but we don't use Bufferreader, WE USE SCANNER. I guess my question is how would i determine if the file the user put exist or contain data values(strings) and also reading the file . I'm a lot confuse>>> thank you>>>


import java.util.*;
import java.io.*;

public class StringTooLongExceptionTestProg
{
   static Scanner console = new Scanner(System.in);
   static final int maxStringSize = 20;

   public static void main(String[] args)
   {
     String filename;
     boolean done;
     boolean eof;
     String str;
     int StringLength;

     done = false;
     eof = false;

     do
     {
       try
       {
           done = true;

               System.out.println(" Enter a file name :");
               filename = console.next();
               Scanner inFile = new Scanner(new FileReader("filename"));



               File f = new File(filename);


               System.out.println();



           while((str = inFile.next()) !=null)
           {
                     System.out.println(str);

               }
               inFile.close();
          System.out.println( " The file name is : " + filename);

       }
       catch( FileNotFoundException fnfRef)
       {
               str = console.next();
               System.out.println(" Exception " + fnfRef.toString()+ " "+ str);
         }

         catch(IOException e)
         {

         }


     }while(!done);


 }




}
CEHJCommented:
You already had the code for reading the filename input. You just need to validate it
yamazoAuthor Commented:
thank you CEHJ. i kind see waht you mean . . I think i did that . There is something I'm missing big time . I just don't understand how can i ask for a file name  and without knowing twhat is the right file name in order not to get a file not found exception.
Running the programm above this is what i have:
////
 Enter a file name :
     J://InputStrinhg.txt
      The file name is : J://InputStrinhg.txt
        Exception java.io.FileNotFoundException: infilename (The system cannot find the
      f ile specified) file cannot  be found
     Enter a file name :
///
this is where i'm confused. please help.
thank you
WebstormCommented:
>>            Scanner inFile = new Scanner(new FileReader("filename"));
>>            File f = new File(filename);

right order should be :

            File f = new File(filename);
            Scanner inFile = new Scanner(new FileReader("filename"));

then :

            File f = new File(filename);
            if (!f.exists())
            {
                   System.err.prinltn("the file doesn't exist");
                   continue; // continue the while loop
            }
            Scanner inFile = new Scanner(new FileReader("filename"));
WebstormCommented:
>> J://InputStrinhg.txt

you mean :
J:\\InputStrinhg.txt

or:
J:/InputStrinhg.txt

WebstormCommented:
I also guess :
J:\\InputString.txt
or:
J:/InputString.txt
WebstormCommented:
>> Scanner inFile = new Scanner(new FileReader("filename"));

shoule be:
  Scanner inFile = new Scanner(new FileReader(filename));

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WebstormCommented:
:-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.