Results 1 to 4 of 4
  1. #1
    moaxjlou is offline Member
    Join Date
    Oct 2008
    Posts
    23
    Rep Power
    0

    Default accessing return values from another class

    Hi
    I have created 2 classes, namely Exam and Student

    Exam has a method generateCandidateNumber(), which returns an array of Strings of the candidate numbers found in an input file.

    I want to access the return values (the array of candidate numbers ) from the Exam class

    How can I do that?

    Here is the code from generateCandidateNumber() in class Exam.
    This method reads from a file input, search for any occurence of a specific pattern, and returns an array of the results based on the pattern. (The array is stored in a variable called "studentsData")
    Java Code:
    public class Exam {
    
    protected static ArrayList<String> studentsData = new ArrayList<String>();
    //....other codes for constructor etc
    //other methods...
    
    
    public ArrayList<String> generateCandidateNumber(){
    		BufferedReader reader = null;
    		Pattern candidateNumberPattern = Pattern.compile("[a-zA-Z]\\d{5}");
    		String line;
    		String result;
    		
    		if (input == null) 
    		return studentsData;
    		
    		else try{
    		
    			reader = new BufferedReader (new FileReader(input));
    			
    			while((line = reader.readLine()) != null){
    				
    				try{
    				Matcher matcher = candidateNumberPattern.matcher(line);
    				
    					while(matcher.find()){
    						result = matcher.group();
    						studentsData.add(result);
    						
    					}
    					
    				}
    				catch (NullPointerException e){
    				System.err.println("Exception occured:");
    				System.out.println(e.getMessage());
    				e.printStackTrace();
    				}
    				
    			}
    		
    		}
    		
    		catch (FileNotFoundException ex) {
                ex.printStackTrace();
            }
    		catch (IOException ex) {
                ex.printStackTrace();
            }
    		 
    		finally {
                //Close the BufferedReader
                try {
                    if (reader != null)
                        reader.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
    	System.out.println(studentsData);
    	return 	studentsData;
    	
    	
    	}
    	
    	//main method goes here...
    	
    	}
    When I run Exam class, it returns the following output:
    Java Code:
    [Z11236, Z11236, Z11856, Z11856, Z12036, Z12036, Z14232, Z14232, Z14523, Z14523,
     Z15102, Z15102, Z15632, Z15632, Z16254, Z16254, Z16325, Z16325, Z18452, Z18452,
     Z19103, Z19103, Z19521, Z19521, Z19630, Z19630, Z20563, Z20563, Z20973, Z20973,
     Z23625, Z23625]
    Student class is like this:
    Java Code:
    public class Student  {
    
    //...the constructor goes here
    
    //other methods go here
    
    public static void main(String[] args){
    
    System.out.println(Exam.studentsData);
    }
    }
    When I run and compile Student.java, the output is as follow:
    Java Code:
    []
    How do I write the code such that the Student class prints out the output as in the Exam class as above?
    Java Code:
    [Z11236, Z11236, Z11856, Z11856, Z12036, Z12036, Z14232, Z14232, Z14523, Z14523,
     Z15102, Z15102, Z15632, Z15632, Z16254, Z16254, Z16325, Z16325, Z18452, Z18452,
     Z19103, Z19103, Z19521, Z19521, Z19630, Z19630, Z20563, Z20563, Z20973, Z20973,
     Z23625, Z23625]

    I would appreciate any help on this?

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    Create an instance of Exams in your Student class:
    Exam e = new Exam(). Read up more about instantiating objects. Access fields/methods in that instance of the class: e.studentsData.

    Then, run whatever operations you wish on that object. Note that a main method will only be run if it is the class explicitly called in the command line. So, you probably wanna move whatever is in your Exam main method to your Student main method.

    That's just a little bit of help, but it's a very important concept that you should really read up on. You may also wanna look into "static" afterwards.

  3. #3
    moaxjlou is offline Member
    Join Date
    Oct 2008
    Posts
    23
    Rep Power
    0

    Default

    HI

    Thanks for your suggestions.I think I should rephrase the questions:

    If you refer to the generateCandidateNumber(), is there a way that I can pass the studentsData to the Student class (mayb through a method)?

    Im thinking that the code should be inserted in this part of the generateCandidateNumber()
    Java Code:
    public ArrayList<String> generateCandidateNumber(){
    System.out.println(studentsData);
    return 	studentsData;// how do i say return studentsData to the Student class?
    }
    but I'm not sure how to implement this. I would appreciate any help on this.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,800
    Rep Power
    25

    Default

    pass the studentsData to the
    The code of the method you posted will return a reference to studentsData to its caller.
    I'm not sure what you mean by "pass". Normally passing means as an argument to a method:
    callAMethod(studentsData); // pass studentsData to callAMethod

Similar Threads

  1. Accessing return value without printing
    By minihazard10 in forum New To Java
    Replies: 2
    Last Post: 10-18-2008, 08:46 AM
  2. Using functions that return values?
    By Megapixelz in forum New To Java
    Replies: 1
    Last Post: 04-30-2008, 05:07 AM
  3. Replies: 4
    Last Post: 04-15-2008, 02:04 PM
  4. how to return values from hashmap
    By oregon in forum New To Java
    Replies: 2
    Last Post: 08-01-2007, 05:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •