calling a method from another class

Reply

Join Date: Apr 2005
Posts: 2
Reputation: sensi is an unknown quantity at this point 
Solved Threads: 0
sensi sensi is offline Offline
Newbie Poster

calling a method from another class

 
0
  #1
Apr 27th, 2005
hi, i have been asked to create a class, TestStudentContactDetails, which uses another classes method. the first class StudentContactDetails contains a method emailAddress(String RegNumber, String Course). This method takes the Course code supplied and opens a csv file stored at c:\UserFiles. It then searches the file for an entry corresponding to the RegNumber supplied. If the entry exists the emailAddress for this entry is returned. If no email address exists an exception iss thrown.

Below is what i have come up with for the TestStudentContactDetails class
  1. /*******************************************************/
  2. /***** Author: David McCabe *** S-ID: 12129103 *****/
  3. /*******************************************************/
  4.  
  5. import java.io.*;
  6. import ContactDetails.*;
  7.  
  8. public class TestStudentContactDetails {
  9.  
  10. //Creates a new instance of TestStudentContactDetails
  11. public TestStudentContactDetails() {
  12. }
  13.  
  14. public static void main (String [] args) {
  15.  
  16. // create instance
  17. StudentContactDetails details = new StudentContactDetails(); // fill in the arguments
  18.  
  19. // call member
  20. try
  21. {
  22. details.emailAddress("12129103", "E410UJ");
  23. }
  24. catch (ContactDetails.StudentContactDetails.IdNotFoundException e)
  25. {
  26. System.out.println("Registration Number not found");
  27. }
  28. catch (ContactDetails.StudentContactDetails.EmailAddressNotAvailableException e)
  29. {
  30. System.out.println("Email Address not found");
  31. }
  32. /*
  33. catch (java.io.FileNotFoundException e)
  34.   {
  35.   System.out.println("java.io.FileNotFoundException thrown");
  36.   }
  37. catch (java.io.IOException e)
  38.   {
  39.   System.out.println("java.io.IOException thrown");
  40.   }
  41. */
  42.  
  43. }// main
  44.  
  45. }// TestStudentContactDetails

The code for the StudentContactDetails class is below:

  1. package ContactDetails;
  2.  
  3. import java.io.*;
  4.  
  5. public class StudentContactDetails
  6. {
  7. /* member class not found */
  8. class EmailAddressNotAvailableException {}
  9.  
  10. /* member class not found */
  11. class IdNotFoundException {}
  12.  
  13.  
  14. public StudentContactDetails()
  15. {
  16. }
  17.  
  18. public String emailAddress(String RegNumber, String Course)
  19. throws IdNotFoundException, EmailAddressNotAvailableException, FileNotFoundException, IOException
  20. {
  21. File inputFile = new File("c:/UserInfo/" + Course + ".csv");
  22. char Separator = ',';
  23. BufferedReader InputAddresses = new BufferedReader(new FileReader(inputFile));
  24. String EmailAddressString = "***ERROR: Email Address not found for " + RegNumber;
  25. String s;
  26. boolean RegNumberFound;
  27. for(RegNumberFound = false; (!RegNumberFound) & ((s = InputAddresses.readLine()) != null);)
  28. {
  29. int FirstSeparatorPos = s.indexOf(',', 0);
  30. int SecondSeparatorPos = s.indexOf(',', FirstSeparatorPos + 1);
  31. String NextRegNumber = s.substring(FirstSeparatorPos + 1, SecondSeparatorPos);
  32. RegNumberFound = true;
  33. int NextSeparatorPos = SecondSeparatorPos;
  34. for(int PosCounter = 3; PosCounter < 9; PosCounter++)
  35. NextSeparatorPos = s.indexOf(',', NextSeparatorPos + 1);
  36.  
  37. int EndSeparatorPos = s.indexOf(',', NextSeparatorPos + 1);
  38. EmailAddressString = s.substring(NextSeparatorPos + 1, EndSeparatorPos);
  39. }
  40.  
  41. if(RegNumberFound)
  42. {
  43. if(!EmailAddressString.equals(""))
  44. return EmailAddressString;
  45. else
  46. throw new EmailAddressNotAvailableException();
  47. } else
  48. {
  49. throw new IdNotFoundException();
  50. }
  51. }
  52. }

I have had to add alot of exceptions handling to t he test class to get it to compile properly but i cant get any result from the emailAddress method call.

Any help would be much apprecicated.

thanks,
sensi
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 10
Reputation: miri is an unknown quantity at this point 
Solved Threads: 0
miri miri is offline Offline
Newbie Poster

Re: calling a method from another class

 
0
  #2
Apr 27th, 2005
Hi!
Please split your big functions to small ones and you will see your problem immediately.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 2
Reputation: sensi is an unknown quantity at this point 
Solved Threads: 0
sensi sensi is offline Offline
Newbie Poster

Re: calling a method from another class

 
0
  #3
Apr 27th, 2005
are you talking about the big functions inthe class StudentCOntactDetails. if you are then i need more advice as this class is not supposed to be changed. The class TestStudentContactDetails is supposed to use the methos emailAddress to return the email ADdress from the csv file as a string and then i am supposed to be ablt to output it to the screen.
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 763
Reputation: Phaelax is on a distinguished road 
Solved Threads: 38
Phaelax Phaelax is offline Offline
Master Poster

Re: calling a method from another class

 
0
  #4
Apr 28th, 2005
  1. for(RegNumberFound = false; (!RegNumberFound) & ((s = InputAddresses.readLine()) != null);)
Perhaps your loop is working as you want because of this "&". I believe that's the bitwise AND operator and you probably want "&&".
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC