| | |
calling a method from another class
![]() |
•
•
Join Date: Apr 2005
Posts: 2
Reputation:
Solved Threads: 0
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
The code for the StudentContactDetails class is below:
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
Below is what i have come up with for the TestStudentContactDetails class
Java Syntax (Toggle Plain Text)
/*******************************************************/ /***** Author: David McCabe *** S-ID: 12129103 *****/ /*******************************************************/ import java.io.*; import ContactDetails.*; public class TestStudentContactDetails { //Creates a new instance of TestStudentContactDetails public TestStudentContactDetails() { } public static void main (String [] args) { // create instance StudentContactDetails details = new StudentContactDetails(); // fill in the arguments // call member try { details.emailAddress("12129103", "E410UJ"); } catch (ContactDetails.StudentContactDetails.IdNotFoundException e) { System.out.println("Registration Number not found"); } catch (ContactDetails.StudentContactDetails.EmailAddressNotAvailableException e) { System.out.println("Email Address not found"); } /* catch (java.io.FileNotFoundException e) { System.out.println("java.io.FileNotFoundException thrown"); } catch (java.io.IOException e) { System.out.println("java.io.IOException thrown"); } */ }// main }// TestStudentContactDetails
The code for the StudentContactDetails class is below:
Java Syntax (Toggle Plain Text)
package ContactDetails; import java.io.*; public class StudentContactDetails { /* member class not found */ class EmailAddressNotAvailableException {} /* member class not found */ class IdNotFoundException {} public StudentContactDetails() { } public String emailAddress(String RegNumber, String Course) throws IdNotFoundException, EmailAddressNotAvailableException, FileNotFoundException, IOException { File inputFile = new File("c:/UserInfo/" + Course + ".csv"); char Separator = ','; BufferedReader InputAddresses = new BufferedReader(new FileReader(inputFile)); String EmailAddressString = "***ERROR: Email Address not found for " + RegNumber; String s; boolean RegNumberFound; for(RegNumberFound = false; (!RegNumberFound) & ((s = InputAddresses.readLine()) != null);) { int FirstSeparatorPos = s.indexOf(',', 0); int SecondSeparatorPos = s.indexOf(',', FirstSeparatorPos + 1); String NextRegNumber = s.substring(FirstSeparatorPos + 1, SecondSeparatorPos); RegNumberFound = true; int NextSeparatorPos = SecondSeparatorPos; for(int PosCounter = 3; PosCounter < 9; PosCounter++) NextSeparatorPos = s.indexOf(',', NextSeparatorPos + 1); int EndSeparatorPos = s.indexOf(',', NextSeparatorPos + 1); EmailAddressString = s.substring(NextSeparatorPos + 1, EndSeparatorPos); } if(RegNumberFound) { if(!EmailAddressString.equals("")) return EmailAddressString; else throw new EmailAddressNotAvailableException(); } else { throw new IdNotFoundException(); } } }
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
•
•
Join Date: Apr 2005
Posts: 2
Reputation:
Solved Threads: 0
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.
•
•
Join Date: Mar 2004
Posts: 763
Reputation:
Solved Threads: 38
Java Syntax (Toggle Plain Text)
for(RegNumberFound = false; (!RegNumberFound) & ((s = InputAddresses.readLine()) != null);)
![]() |
Similar Threads
- Selection Sort in java (Java)
- First JLIST, used with GUI Inventory (Java)
- Class & method Boolean (Java)
- a class question (Python)
- calling a graphics method (Java)
Other Threads in the Java Forum
- Previous Thread: Only 1 instance of program
- Next Thread: while loops
| Thread Tools | Search this Thread |
911 actionlistener addball addressbook android api append applet application array arrays automation binary bluetooth button character chat class client code component consumer css csv database desktop eclipse ee error fractal ftp game givemetehcodez graphics gui html ide image integer j2me japplet java javaarraylist javac javaee javaprojects jni jpanel julia jvm linked linux list loan mac map method methods mobile netbeans newbie objects online oriented output panel phone printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server service set sms software sort sql string swing test threads time transfer tree ubuntu update windows working





