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

/*******************************************************/
/*****  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:

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

Hi!
Please split your big functions to small ones and you will see your problem immediately.

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.

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 "&&".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.