[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Handling an exception

Posted on 2006-04-10
12
Medium Priority
?
1,324 Views
Last Modified: 2008-02-01
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.
0
Comment
Question by:yamazo
  • 6
  • 2
  • 2
  • +2
12 Comments
 
LVL 6

Expert Comment

by:avinthm
ID: 16423713
First of all - to throw StringTooLongException, you need to create a class StringTooLongException which extends Exception class
0
 
LVL 92

Expert Comment

by:objects
ID: 16423746
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16424042
You need to make sure the filename is long enough. You need to open the file with the name the user chooses
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Expert Comment

by:Webstorm
ID: 16425035
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
 

Author Comment

by:yamazo
ID: 16432013
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16432032
You already had the code for reading the filename input. You just need to validate it
0
 

Author Comment

by:yamazo
ID: 16432552
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
 
LVL 13

Assisted Solution

by:Webstorm
Webstorm earned 2000 total points
ID: 16434321
>>            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
 
LVL 13

Expert Comment

by:Webstorm
ID: 16434327
>> J://InputStrinhg.txt

you mean :
J:\\InputStrinhg.txt

or:
J:/InputStrinhg.txt

0
 
LVL 13

Expert Comment

by:Webstorm
ID: 16434333
I also guess :
J:\\InputString.txt
or:
J:/InputString.txt
0
 
LVL 13

Accepted Solution

by:
Webstorm earned 2000 total points
ID: 16434342
>> Scanner inFile = new Scanner(new FileReader("filename"));

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

Expert Comment

by:Webstorm
ID: 16631476
:-)
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month19 days, 23 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question