Results 1 to 5 of 5
Thread: Creating Array of Objects
- 12-03-2009, 12:02 PM #1
Member
- Join Date
- Dec 2009
- Location
- Colombo, Sri Lanka
- Posts
- 2
- Rep Power
- 0
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 inAlso it gives an error if I type this inside the main methodJava Code:System.out.print("\nStudent ID : "+sSet1[sId].getId()); System.out.print("\nStudent Grade : "+sSet1[sId].getGrade);...Please someone help me....Java Code:student[] sSet1 = new student[500];
(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.
- 12-03-2009, 12:31 PM #2
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
- 12-03-2009, 01:40 PM #3
Member
- Join Date
- Dec 2009
- Location
- Colombo, Sri Lanka
- Posts
- 2
- Rep Power
- 0
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...
Please if you have time tell me about Vector of Objects...Java Code:Vector<Object> vector = new Vector<Object>();
Thank You again for reading the long code and reply...
Thanks..!
-ChathuraLast edited by chathurajeewaka; 12-03-2009 at 01:58 PM.
- 12-03-2009, 01:58 PM #4
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
- 12-03-2009, 03:23 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
And then ignore it should you choose Vector, and use ArrayList instead.
Vector should really be looked at as deprecated.
Similar Threads
-
Creating an Array of Objects
By int80 in forum New To JavaReplies: 4Last Post: 08-09-2011, 12:40 PM -
Creating an array of objects
By geowizard in forum New To JavaReplies: 5Last Post: 11-16-2009, 01:25 AM -
Creating and using java objects at runtime...
By baran_khan in forum Advanced JavaReplies: 5Last Post: 08-28-2008, 05:36 PM -
Creating an array of nonprimitive objects
By Java Tip in forum java.langReplies: 0Last Post: 04-14-2008, 08:46 PM -
Creating objects question
By sergm in forum New To JavaReplies: 2Last Post: 12-27-2007, 04:10 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks