Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2009
    Location
    Colombo, Sri Lanka
    Posts
    2
    Rep Power
    0

    Default Creating Array of Objects

    here are the two .java files I made to test...
    But it dosent compile...gives an error that sSet1 variable doesn't exist in
    Java Code:
    System.out.print("\nStudent ID : "+sSet1[sId].getId());
    System.out.print("\nStudent Grade : "+sSet1[sId].getGrade);
    Also it gives an error if I type this inside the main method
    Java Code:
    student[] sSet1 = new student[500];
    ...Please someone help me....
    (I have never tried,heard or learned about array of objects before today morning..!)
    I`m sorry that the code is a bit long...
    Thank You...
    -------------------------------------------------------------------------
    Java Code:
    class student{
    	//Instance Variables
    	private float subject1, subject2, subject3, average;
    	private int id;
    	private String grade;
    	
            //Constructor Methods
    	student (int a){
    		id = a;
    		subject1 = subject2 = subject3 = 0;
    		average = 0;
    		grade = "Pending";
    		
    	}
    		
    	public void setSubject1(float a){
    		subject1 = a;
    	}
    	
    	public void setSubject2(float a){
    		subject2 = a;
    	}
    	
    	public void setSubject3(float a){
    		subject3 = a;
    	}
    	
    	public String getGrade(){
    		return grade;
    	}
    
    	public int getId(){
    		return id;
    	}
    
    }
    --------------------------------------------------------------------------
    Java Code:
    import java.io.*;
    public class aStudent{
    	
    	static int sId;
    	private static int numOfStudents = 0;
    	
    	//a method -> to create students
    	private static void isAStudent(int b){
    	
    		//I took the name sSet1 in the meaning of 'Student Set 1'
    		student[] sSet1 = new student[500];
    		sSet1[b] = new student(b);
    		numOfStudents++;
    		
    	}
    	public static void main (String arg[]){
    
    		InputStreamReader isr= new InputStreamReader(System.in);
    		BufferedReader reader=new BufferedReader(isr);
    		//student[] sSet1 = new student[500];
    		
    		try{	System.out.print("\nStudent ID : ");
    			sId = Integer.parseInt(reader.readLine());
    			
    			isAStudent(sId);
    			
    			System.out.print("\nStudent ID : "+sSet1[sId].getId());
    			System.out.print("\nStudent Grade : "+sSet1[sId].getGrade);
    			
    		}catch(IOException e){
    				System.out.println(e.getMessage());
    			}
    		
    	}
    
    }
    ------------------------------------------------------------------------
    Thank You...
    Last edited by chathurajeewaka; 12-03-2009 at 12:13 PM.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

    Default

    sSet1 is out of scope for the method(s). Declare the array where you declared sId and numOfStudents, and only there. That is not an array problem, but a general problem of scope.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    Join Date
    Dec 2009
    Location
    Colombo, Sri Lanka
    Posts
    2
    Rep Power
    0

    Default

    Thank You my friend...
    So I will have to place all those things in one place(one method).

    Well I was thinking about can I use a Vector other than using an array..
    I mean, is there Vectors of Arrays.
    Because I need this program to be able to enter many data as you need...
    Searching in the internet gave me this...
    would anyone mind about telling me what this code below is...
    Java Code:
        Vector<Object> vector = new Vector<Object>();
    Please if you have time tell me about Vector of Objects...

    Thank You again for reading the long code and reply...
    Thanks..!
    -Chathura
    Last edited by chathurajeewaka; 12-03-2009 at 01:58 PM.

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

    Default

    You should read the Vector API docs first: Vector (Java 2 Platform SE 5.0)
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default

    And then ignore it should you choose Vector, and use ArrayList instead.

    Vector should really be looked at as deprecated.

Similar Threads

  1. Creating an Array of Objects
    By int80 in forum New To Java
    Replies: 4
    Last Post: 08-09-2011, 12:40 PM
  2. Creating an array of objects
    By geowizard in forum New To Java
    Replies: 5
    Last Post: 11-16-2009, 01:25 AM
  3. Creating and using java objects at runtime...
    By baran_khan in forum Advanced Java
    Replies: 5
    Last Post: 08-28-2008, 05:36 PM
  4. Creating an array of nonprimitive objects
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:46 PM
  5. Creating objects question
    By sergm in forum New To Java
    Replies: 2
    Last Post: 12-27-2007, 04:10 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
  •