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?
 
WebstormCommented:
>> Scanner inFile = new Scanner(new FileReader("filename"));

shoule be:
  Scanner inFile = new Scanner(new FileReader(filename));
0
 
avinthmCommented:
First of all - to throw StringTooLongException, you need to create a class StringTooLongException which extends Exception class
0
 
objectsCommented:
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
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
CEHJCommented:
You need to make sure the filename is long enough. You need to open the file with the name the user chooses
0
 
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()+")");
0
 
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);


 }




}
0
 
CEHJCommented:
You already had the code for reading the filename input. You just need to validate it
0
 
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
0
 
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"));
0
 
WebstormCommented:
>> J://InputStrinhg.txt

you mean :
J:\\InputStrinhg.txt

or:
J:/InputStrinhg.txt

0
 
WebstormCommented:
I also guess :
J:\\InputString.txt
or:
J:/InputString.txt
0
 
WebstormCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.