4 Replies - 428 Views - Last Post: 16 October 2010 - 10:46 AM Rate Topic: -----

#1 sam1012  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 01-May 09

array of objects and their mothod calling

Posted 16 October 2010 - 09:41 AM

i have declared an array of objects, and i have input method in my class,
now in main method i want to call that methods, but it is not working, any help please?

import java.util.*;
public class Task_A
{

	public static void main(String args[])
	{
		Student st[];
		st = new Student[3];

		for(int i = 0 ; i < 3 ; i++)
		{
			st[i].input();
		}
		for(int j = 0 ; j < 3 ; j++)
		{
			st[j].output();
		}

		
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: array of objects and their mothod calling

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: array of objects and their mothod calling

Posted 16 October 2010 - 09:52 AM

Show us the Student class. Also, each index needs to be initialized: st[indexValue] = new Student();
Was This Post Helpful? 0
  • +
  • -

#3 sam1012  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 01-May 09

Re: array of objects and their mothod calling

Posted 16 October 2010 - 10:17 AM

View Postsam1012, on 16 October 2010 - 08:41 AM, said:

i have declared an array of objects, and i have input method in my class,
now in main method i want to call that methods, but it is not working, any help please?

import java.util.*;
public class Task_A
{

	public static void main(String args[])
	{
		Student st[];
		st = new Student[3];

		for(int i = 0 ; i < 3 ; i++)
		{
			st[i].input();
		}
		for(int j = 0 ; j < 3 ; j++)
		{
			st[j].output();
		}

		
	}
}


[code]
import java.util.*;
public class Student extends Person
{
private String course;


public Student()
{

super();
course = "";

}

public Student(String nam, int ag, String cours)
{

super(nam,ag);
course = cours;

}

public void input()
{
super.input();
System.out.println(" Enter the Course : ");
Scanner in = new Scanner(System.in);
course = in.next();


}

public void output()
{
super.output();

System.out.println(" The Course : " + course);
}

}
[code\]

Was This Post Helpful? 0
  • +
  • -

#4 bobjob  Icon User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • Posts: 163
  • Joined: 29-March 08

Re: array of objects and their mothod calling

Posted 16 October 2010 - 10:25 AM

the problem is you havnt called the constructor.

in the first loop (using int 'i').
do something like
st[i] = new Student();

before
st[i].input();
Was This Post Helpful? 0
  • +
  • -

#5 sam1012  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 01-May 09

Re: array of objects and their mothod calling

Posted 16 October 2010 - 10:45 AM

View Postbobjob, on 16 October 2010 - 09:25 AM, said:

the problem is you havnt called the constructor.

in the first loop (using int 'i').
do something like
st[i] = new Student();

before
st[i].input();


oh i got it, thank you so much, now it is working.... :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1