Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

IOException....try...catch...

Thread view: 
bhavish1987@gmail.com - 26 Mar 2007 09:01 GMT
Dear All,
           can someone please post me the correct code for the
following code. I'd like to put the condition, mark must be minimum 0
and maximum 100 using Exception ( try ... catch ).

/*
  This Java program list all the marks which are outside the
specified
  range and need the lecturer's attention, once all the marks have
been entered.
*/

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.sql.Array;
/*
* Task3.java
*
*
*/

/**
*
* @author Bhavish Sharma Hurreeram© - psychoma...@hotmail.com
*/

public class Task3 {

   private String name;
   private String mark;

   /** Creates a new instance of Task3 */
   public Task3() {

   }

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

    public String getMark() {
       return mark;
   }

   public void setMark(String mark) {
       this.mark = mark;
   }

    public static void main(String args []) throws IOException{

         Task3 task3 = new Task3();
         task3.readFile();
   }

   public void readFile() throws FileNotFoundException, IOException
   {
         String line="";
         int marks=0;
         BufferedReader in = new BufferedReader(new
FileReader("MarkFile.txt"));

         while (( line = in.readLine()) != null){
             name = line.substring(0,48);
             mark = line.substring(48).trim();
             try{
               marks=Integer.parseInt(mark);
                         }

                        catch(Exception e){
                               //System.out.println("An Erroneous
mark");
                               displayErrors();
                        }

            for(int i=name.length();i<48;i++) {
              name += " ";
            }

            for(int i=mark.length();i<7;i++) {
              mark += " ";
            }

            setName(name);
            setMark(mark);

            if(marks < 0 || marks > 100){
                               displayErrors();
            }

               }

       }

   public void displayErrors()
   {
       System.out.println(getName() + "  " + getMark() + " An
Erroneous mark");
   }
Andrew Thompson - 26 Mar 2007 09:37 GMT
On Mar 26, 6:01 pm, bhavish1...@gmail.com wrote:
> Dear All,
>             can someone please post me the correct code for the
> following code.

Damn.  I should have checked this one
before sending that last post.  Apparently
you are not trolling.

On the other hand, you seem to think of
this discussion group as some sort of
'help desk'.  That is not how it works.

For you to get any benefit from these
'technical discussion forums', you need
to ask specific questions, that show you
have done some of the groundwork.

Andrew T.
Jeff Higgins - 27 Mar 2007 01:54 GMT
bhavish wrote:
> Dear All,
>             can someone please post me the correct code for the
> following code. I'd like to put the condition, mark must be minimum 0
> and maximum 100 using Exception ( try ... catch ).

I'm not sure I've understood your request, but...

Alice                                            -1
John Smith                                       50
Mary Lou                                         70
Alexander Popandopula                            90
Zeppo                                            101

Alice                                             -1 An Erroneous mark
Zeppo                                             101 An Erroneous mark

import java.io.*;

public class Task3 {

 private String name;
 private String mark;

 public static void main(String args []) throws IOException {

   Task3 task3 = new Task3();
   task3.readFile();
 }

 public void readFile()
 throws FileNotFoundException, IOException {
   String line;
   int marks;
   BufferedReader in = new BufferedReader(new FileReader("MarkFile.txt"));

   while (( line = in.readLine()) != null && line.length() > 48){
     name = line.substring(0,48);
     mark = line.substring(48).trim();
     try{
       marks=Integer.parseInt(mark);
       if(marks < 0 || marks > 100){
         throw new IllegalArgumentException();
       }
     }
     catch(Exception e){
       displayErrors();
     }
   }
 }

 public void displayErrors() {
   System.out.println(name + "  " + mark + " An Erroneous mark");
 }
}
Jeff Higgins - 27 Mar 2007 03:58 GMT
>    while (( line = in.readLine()) != null && line.length() > 48){
>      name = line.substring(0,48);
[quoted text clipped - 15 lines]
>  }
> }

   while (( line = in.readLine()) != null && line.length() > 48){
     name = line.substring(0,48);
     mark = line.substring(48).trim();
     try{
       marks=Integer.parseInt(mark);
       if(marks < 0 || marks > 100){
         throw new IllegalArgumentException();
       }
     }
     catch(NumberFormatException e){
       displayErrors("Unable to parse an Integer");
     }
     catch(IllegalArgumentException e){
       displayErrors(name + "  " + mark + " An Erroneous mark");
     }
   }
 }

 public void displayErrors(String s) {
   System.out.println(s);
 }
}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.