473,809 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reading a text file

50 New Member
Abstract Class, Inheritance, Polymorphism, File Handling, Exception Handing
A program is to be devised for a university to work out whether or not, a student or a professor, is
outstanding. The criterion for a professor to be outstanding is to have over 120 publications. The
criterion for a student to be outstanding is to have a Grade Point Average of 3.3.
For a professor, the three class data members would be name, number of publications and the
method is Outstanding().
For a student, the three class data members would be name, grade point average and the method is
Outstanding().
In order to test the program, use a text file which holds 100 records such as:
P Brian Keen 79
S Tom Kelly 3.7
P Jim Zub 95
P Tim Norman 130
S Barbara Maddil 2.2
In the above list, ‘P’ stands for a professor and ‘S’ stands for a student. As examples, according to
the given criteria, Tim Norman is the outstanding professor and Tom Kelly is the outstanding
student.
Design, implement and test the necessary classes to implement the given scenario.

I' ve done this code:

Expand|Select|Wrap|Line Numbers
  1. public abstract class SchoolMember {
  2.  
  3.     //These are the attributes for a SchoolMember class
  4.  
  5.     /**
  6.      * This is the type of member 
  7.      * either a professor or a student.
  8.      */
  9.     String typeOfMember;
  10.  
  11.     /**
  12.      * This is the name of the school member
  13.      * i.e. the name of the professor or the 
  14.      * student.  It is of type string.
  15.      */
  16.     String name;
  17.  
  18.     /**
  19.      * This is the surname of the school member
  20.      * i.e. the surname of the professor or the
  21.      * student.  It is of type string.
  22.      */
  23.     String surname;
  24.  
  25.     /**
  26.      * This is the score of the SchoolMember. 
  27.      * In the case of a student it is the average
  28.      * while in the case of a professor it is the
  29.      * number of publications.
  30.      */
  31.     String score;
  32.  
  33.         public abstract boolean outstanding(String s);
  34.  
  35. }
Professor class

Expand|Select|Wrap|Line Numbers
  1. public class Professor extends SchoolMember {
  2.  
  3.     Double  noOfPublications;
  4.     boolean isOutstanding;
  5.  
  6.     public Professor(Double score){
  7.  
  8.  
  9.         noOfPublications = score;
  10.  
  11.     }
  12.     public boolean outstanding (String s) {
  13.  
  14.         if (noOfPublications >120 ) {
  15.  
  16.             isOutstanding = true;
  17.         }
  18.         return isOutstanding;
  19.  
  20.     }
  21.  
  22.  
  23. }
Expand|Select|Wrap|Line Numbers
  1. Student class
  2.  
  3. public class Student extends SchoolMember {
  4.     /**
  5.      * This is the grade point average of the student.
  6.      * It is of type double.
  7.      */
  8.     Double gradePointAverage;
  9.  
  10.     /**
  11.      * This is an attribute of type boolean
  12.      * where you check if the student is outstanding or not.
  13.      */
  14.     Boolean isOutstanding;
  15.  
  16.     /**
  17.      * @param score of type double which takes the integer
  18.      * value from the text file.
  19.      * 
  20.      * The score value taken from the text file is assigned to
  21.      * the variable gradePointAverage
  22.      */
  23.     public Student(Double score) {
  24.  
  25.         gradePointAverage = score;
  26.  
  27.     }
  28.  
  29.     /**
  30.      * if the gradePointAverage is greater then 3.3 therefore the student
  31.      * is outstanding.  If it is smaller is not outstanding.
  32.      * 
  33.      * @param s the input string from the text file
  34.      * @return boolean if either the student is outstanding or not
  35.      * 
  36.      */
  37.     public boolean outstanding(String s) {
  38.  
  39.         if (gradePointAverage > 3.3) {
  40.  
  41.             isOutstanding = true;
  42.         }
  43.  
  44.         return true;
  45.     }
  46.  
  47. }
FileParser Class
Expand|Select|Wrap|Line Numbers
  1. public class FileParser {
  2.  
  3.     public SchoolMember[] convert(String s) {
  4.  
  5.         FileReader f = null;
  6.         BufferedReader buf = null;
  7.  
  8.         try {
  9.             f = new FileReader(s);
  10.             buf = new BufferedReader(f);
  11.  
  12.             String line = buf.readLine();
  13.  
  14.             while (line != null) {
  15.  
  16.                 String[] datum = line.split(" ");
  17.  
  18.                 SchoolMember member;
  19.                 String memberType = datum[0];
  20.                 Double score = Double.parseDouble(datum[3]);
  21.  
  22.                 /**
  23.                  * if the memberType is equal to P
  24.                  * i.e. it is a professor therefore the data found in
  25.                  * the text file is inserted into the professors class
  26.                  * to calculate if that professor is outstanding or not.
  27.                  */
  28.                 if (memberType.equals("P") ) {
  29.  
  30.                     member = new Professor(score);
  31.                     member.typeOfMember = memberType;
  32.                     member.name = datum[1];
  33.                     member.surname = datum[2];
  34.                 }
  35.  
  36.                 /**
  37.                  * if the memberType is equal to S
  38.                  * i.e. it is a sudent therefore the data found in the 
  39.                  * text file is inserted into the students class
  40.                  * to calculate if the student is outstanding or not.
  41.                  */
  42.                 else if (memberType.equals("S")) {
  43.  
  44.                     member = new Student(score);
  45.                     member.typeOfMember = memberType;
  46.                     member.name = datum[1];
  47.                     member.surname = datum[2];
  48.                 }
  49.             }
  50.         }
  51.  
  52.         catch (FileNotFoundException e) {
  53.             // TODO Auto-generated catch block
  54.             e.printStackTrace();
  55.         } catch (IOException e) {
  56.             // TODO Auto-generated catch block
  57.             e.printStackTrace();
  58.         } finally {
  59.             try {
  60.                 if (buf != null) {                 
  61.                     buf.close();
  62.                 }
  63.  
  64.                 buf.close();
  65.                 f.close();
  66.             }
  67.  
  68.             catch (IOException e) {
  69.                 // just ignore
  70.             }
  71.         }
  72.  
  73.         return null;
  74.  
  75.     }
  76. }
Launcher

Expand|Select|Wrap|Line Numbers
  1. public class Launcher {
  2.  
  3.     /**
  4.      * @param args
  5.      */
  6.     public static void main(String[] args) {
  7.     FileParser convertor = new FileParser();
  8.     convertor.convert("c:/text.txt");
  9.  
  10.     }
  11.  
  12. }
When I run this code nothing happens. Please can some1 correct the code and tell me whats wrong with it.

Thanks very much

Christine
Oct 9 '07 #1
13 1886
RedSon
5,000 Recognized Expert Expert
Did you step through the code and trace its execution? What did you come up with? Is there no exception that get thrown? Does it compile properly?
Oct 9 '07 #2
sugard
50 New Member
yes it compiles normally without giving any errors. since i am a beginner i ve never used debugger before.
Oct 9 '07 #3
RedSon
5,000 Recognized Expert Expert
yes it compiles normally without giving any errors. since i am a beginner i ve never used debugger before.
What IDE are you using?
Oct 9 '07 #4
sugard
50 New Member
i am using java eclipse.
Oct 9 '07 #5
RedSon
5,000 Recognized Expert Expert
i am using java eclipse.
In eclipse set a breakpoint inside your main() method and then click on debug. It should execute then pause execution when your breakpoint is reached.
Oct 9 '07 #6
sugard
50 New Member
it did not work .. i dont know..
Oct 9 '07 #7
RedSon
5,000 Recognized Expert Expert
it did not work .. i dont know..
you have this:

Expand|Select|Wrap|Line Numbers
  1. public class Launcher {
  2.  
  3.     /**
  4.      * @param args
  5.      */
  6.     public static void main(String[] args) {
  7.     FileParser convertor = new FileParser();
  8.     convertor.convert("c:/text.txt");
  9.  
  10.     }
  11.  
  12. }
And you put a breakpoint on this line:

Expand|Select|Wrap|Line Numbers
  1. FileParser convertor = new FileParser();
And nothing happend?

Did you make sure to choose debug instead of just run?
Oct 9 '07 #8
sugard
50 New Member
Ok when i did that under the names coloumn there was writter 'args' and under value 'String[0] (id =16) ' and underneath there was displayed this '[]'
Oct 9 '07 #9
RedSon
5,000 Recognized Expert Expert
Ok when i did that under the names coloumn there was writter 'args' and under value 'String[0] (id =16) ' and underneath there was displayed this '[]'
Ok good looks like your debugger is working, now you just need to step through your code and try it out. How long have you been programming java?
Oct 9 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

6
12752
by: Suresh Kumaran | last post by:
Hi All, Does anybody know the sytax in VB.NET to write the contents of a multiline text box to a text file? Appreciate help. Suresh
1
7060
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the file... Thank you to all of you who can help me with this one...
19
10388
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much text is available until I have read it... which seems to imply that multiple reads of the input stream will be inevitable. Now I can correctly find the number of characters available by: |
0
1757
by: Eric Lilja | last post by:
Hello, I have a text file that contains a number of entries describing a recipe. Each entry consists of a number of strings. Here's an example file with only one entry (recipe): Name=Maple Quill Process=Interim Level=10 Technique=Fletching Knowledge=Woodworking Device=Sawhorse Primary components=Refined Maple
1
6765
by: Magnus | last post by:
allrite folks, got some questions here... 1) LAY-OUT OF REPORTS How is it possible to fundamentaly change the lay-out/form of a report in access? I dont really know it that "difficult", but listen up; Reports, the way I look at them, all present data downwards, in this way; TITLE data
50
5048
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem -- the character input is a different matter, at least if I want to remain within the bounds of the standard library.
2
2501
by: Sabin Finateanu | last post by:
Hi I'm having problem reading a file from my program and I think it's from a procedure I'm using but I don't see where I'm going wrong. Here is the code: public bool AllowUsage() { OperatingSystem os = Environment.OSVersion; AppDomain ad = Thread.GetDomain();
4
3308
by: dale zhang | last post by:
Hi, I am trying to save and read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp Right now, I saved images without any errors. After reading the ole object from db, I saved it to C: as file1.bmp and displayed on the web. But it can not be displayed. After I manually sent the file to wordpad, it shows
4
12812
by: Amit Maheshwari | last post by:
I need to read text file having data either comma seperated or tab seperated or any custom seperator and convert into a DataSet in C# . I tried Microsoft Text Driver and Microsoft.Jet.OLEDB.4.0 to read text file but could not get the data in correct format. All columns are not coming in dataset and rows are messing up. Suggestions please ???
3
2839
by: The Cool Giraffe | last post by:
Regarding the following code i have a problem. void read () { fstream file; ios::open_mode opMode = ios::in; file.open ("some.txt", opMode); char *ch = new char; vector <charv; while (!file.eof ()) { do {
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10640
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10387
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7662
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5689
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.