Reading a text file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sugard
    New Member
    • Aug 2007
    • 50

    Reading a text file

    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:

    Code:
    public abstract class SchoolMember {
    	
    	//These are the attributes for a SchoolMember class
    	
    	/**
    	 * This is the type of member 
    	 * either a professor or a student.
    	 */
    	String typeOfMember;
    	
    	/**
    	 * This is the name of the school member
    	 * i.e. the name of the professor or the 
    	 * student.  It is of type string.
    	 */
    	String name;
    	
    	/**
    	 * This is the surname of the school member
    	 * i.e. the surname of the professor or the
    	 * student.  It is of type string.
    	 */
    	String surname;
    	
    	/**
    	 * This is the score of the SchoolMember. 
    	 * In the case of a student it is the average
    	 * while in the case of a professor it is the
    	 * number of publications.
    	 */
    	String score;
    	
    		public abstract boolean outstanding(String s);
    	  
    }
    Professor class

    Code:
    public class Professor extends SchoolMember {
    
        Double  noOfPublications;
    	boolean isOutstanding;
    	
    	public Professor(Double score){
    		
    			
    		noOfPublications = score;
    		
    	}
    	public boolean outstanding (String s) {
    		
    		if (noOfPublications >120 ) {
    			
    			isOutstanding = true;
    		}
    		return isOutstanding;
    		
    	}
    	
    		
    }
    Code:
    Student class
    
    public class Student extends SchoolMember {
    	/**
    	 * This is the grade point average of the student.
    	 * It is of type double.
    	 */
    	Double gradePointAverage;
    	
    	/**
    	 * This is an attribute of type boolean
    	 * where you check if the student is outstanding or not.
    	 */
    	Boolean isOutstanding;
    	
    	/**
    	 * @param score of type double which takes the integer
    	 * value from the text file.
    	 * 
    	 * The score value taken from the text file is assigned to
    	 * the variable gradePointAverage
    	 */
    	public Student(Double score) {
    		
    		gradePointAverage = score;
    		
    	}
    	
    	/**
    	 * if the gradePointAverage is greater then 3.3 therefore the student
    	 * is outstanding.  If it is smaller is not outstanding.
    	 * 
    	 * @param s the input string from the text file
    	 * @return boolean if either the student is outstanding or not
    	 * 
    	 */
    	public boolean outstanding(String s) {
    		
    		if (gradePointAverage > 3.3) {
    			
    			isOutstanding = true;
    		}
    		
    		return true;
    	}
    	
    }
    FileParser Class
    Code:
    public class FileParser {
    
    	public SchoolMember[] convert(String s) {
    	
    		FileReader f = null;
    		BufferedReader buf = null;
    		
    		try {
    			f = new FileReader(s);
    			buf = new BufferedReader(f);
    		
    			String line = buf.readLine();
    		
    			while (line != null) {
    		
    				String[] datum = line.split(" ");
    		
    				SchoolMember member;
    				String memberType = datum[0];
    				Double score = Double.parseDouble(datum[3]);
    				
    				/**
    				 * if the memberType is equal to P
    				 * i.e. it is a professor therefore the data found in
    				 * the text file is inserted into the professors class
    				 * to calculate if that professor is outstanding or not.
    				 */
    				if (memberType.equals("P") ) {
    	    	
    					member = new Professor(score);
    					member.typeOfMember = memberType;
    					member.name = datum[1];
    					member.surname = datum[2];
    				}
    			
    				/**
    				 * if the memberType is equal to S
    				 * i.e. it is a sudent therefore the data found in the 
    				 * text file is inserted into the students class
    				 * to calculate if the student is outstanding or not.
    				 */
    				else if (memberType.equals("S")) {
    
    					member = new Student(score);
    					member.typeOfMember = memberType;
    					member.name = datum[1];
    					member.surname = datum[2];
    				}
    			}
    		}
    	
    		catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} finally {
    			try {
    				if (buf != null) { 				
    					buf.close();
    				}
    						
    			    buf.close();
    			    f.close();
    			}
    					
    			catch (IOException e) {
    				// just ignore
    			}
    		}
    
    		return null;
    
    	}
    }
    Launcher

    Code:
    public class Launcher {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    	FileParser convertor = new FileParser();
    	convertor.convert("c:/text.txt");
    
    	}
    
    }
    When I run this code nothing happens. Please can some1 correct the code and tell me whats wrong with it.

    Thanks very much

    Christine
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    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?

    Comment

    • sugard
      New Member
      • Aug 2007
      • 50

      #3
      yes it compiles normally without giving any errors. since i am a beginner i ve never used debugger before.

      Comment

      • RedSon
        Recognized Expert Expert
        • Jan 2007
        • 4980

        #4
        Originally posted by sugard
        yes it compiles normally without giving any errors. since i am a beginner i ve never used debugger before.
        What IDE are you using?

        Comment

        • sugard
          New Member
          • Aug 2007
          • 50

          #5
          i am using java eclipse.

          Comment

          • RedSon
            Recognized Expert Expert
            • Jan 2007
            • 4980

            #6
            Originally posted by sugard
            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.

            Comment

            • sugard
              New Member
              • Aug 2007
              • 50

              #7
              it did not work .. i dont know..

              Comment

              • RedSon
                Recognized Expert Expert
                • Jan 2007
                • 4980

                #8
                Originally posted by sugard
                it did not work .. i dont know..
                you have this:

                Code:
                public class Launcher {
                
                	/**
                	 * @param args
                	 */
                	public static void main(String[] args) {
                	FileParser convertor = new FileParser();
                	convertor.convert("c:/text.txt");
                
                	}
                
                }
                And you put a breakpoint on this line:

                Code:
                FileParser convertor = new FileParser();
                And nothing happend?

                Did you make sure to choose debug instead of just run?

                Comment

                • sugard
                  New Member
                  • Aug 2007
                  • 50

                  #9
                  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 '[]'

                  Comment

                  • RedSon
                    Recognized Expert Expert
                    • Jan 2007
                    • 4980

                    #10
                    Originally posted by sugard
                    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?

                    Comment

                    • RedSon
                      Recognized Expert Expert
                      • Jan 2007
                      • 4980

                      #11
                      Originally posted by RedSon
                      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?
                      sugard,

                      It might be time for you to read some other stuff. Like http://pages.cs.wisc.edu/~cs302/reso...l/0-start.html or http://linuxdevices.com/articles/AT6046208714.html. And maybe talk to your professor, computer lab tech, or fellow student to ask them to help you learn the debugger.

                      Comment

                      • sugard
                        New Member
                        • Aug 2007
                        • 50

                        #12
                        i've been programming for about these last 2 months. I am learning java through tutorials and this is a task from an assignment. When I ve gone through the code nothing happened...

                        Comment

                        • r035198x
                          MVP
                          • Sep 2006
                          • 13225

                          #13
                          Originally posted by sugard
                          i've been programming for about these last 2 months. I am learning java through tutorials and this is a task from an assignment. When I ve gone through the code nothing happened...
                          Your while loop doesn't look good.
                          1.) You are not handling all the possible cases.
                          2.) You don't seem to have made it possible for your while loop to be exited once it has been entered. Is there anywhere you are modifying the value of line.

                          P.S I'll have to rename this thread because your title does not describe your problem at all.

                          Comment

                          • sugard
                            New Member
                            • Aug 2007
                            • 50

                            #14
                            ok i will try to fix it. thanks

                            Comment

                            Working...