944,117 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 40791
  • Java RSS
Apr 27th, 2005
0

calling a method from another class

Expand Post »
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
Java Syntax (Toggle Plain Text)
  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:

Java Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sensi is offline Offline
2 posts
since Apr 2005
Apr 27th, 2005
0

Re: calling a method from another class

Hi!
Please split your big functions to small ones and you will see your problem immediately.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
miri is offline Offline
10 posts
since Apr 2005
Apr 27th, 2005
0

Re: calling a method from another class

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sensi is offline Offline
2 posts
since Apr 2005
Apr 28th, 2005
0

Re: calling a method from another class

Java Syntax (Toggle Plain Text)
  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 "&&".
Reputation Points: 92
Solved Threads: 51
Practically a Posting Shark
Phaelax is offline Offline
856 posts
since Mar 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Only 1 instance of program
Next Thread in Java Forum Timeline: while loops





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC