4 Replies - 246 Views - Last Post: 30 March 2011 - 12:28 PM Rate Topic: -----

#1 tvenhc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 30-March 11

Calling an array inside main function.. Urgent help

Posted 30 March 2011 - 05:01 AM

Hello, I am a beginner in Java and I would like to get some help on this. What I am trying to do is to call Film[] Array inside the main() method so I can view different fields of Film[].
For example, each array position will contain 5 fields, but I would like to get a list of the first field via getTitle() method.
So what I want to get out of it is a list of movie titles.
I tried few different approach but couldn't figure it out myself.
Please help me to get through this. :D
Thanks in advance!

class Film
{
	private String filmTitle;
	private boolean filmFormatWide;
	private boolean filmFormatFull;
	private boolean filmSpecial;
	private String filmThumbnail;

	Film(String title, boolean fWide, boolean fFull,boolean fSpecial,String fThumbnail)
	{
	
		 filmTitle = title;
		 filmFormatWide = fWide;
		 filmFormatFull = fFull;
		 filmSpecial = fSpecial;
		 filmThumbnail = fThumbnail;	
	}
	
	public void setTitle(String title)
	{
		filmTitle = title;
	}
	
	public void setFormatWide(boolean fWide)
	{
		filmFormatWide = fWide;
	}
	
	public void setFormatFull(boolean fFull)
	{
		filmFormatFull = fFull;
	}
	
	public void setSpecial(boolean fSpecial)
	{
		filmSpecial = fSpecial;
	}
	
	public void setThumbnail(String fThumbnail)
	{
		filmThumbnail = fThumbnail;
	}
	
	public String getTitle()
	{
		return filmTitle;
	}
	
	public boolean getFormatWide()
	{
		return filmFormatWide;
	}
	
	public boolean getFormatFull()
	{
		return filmFormatFull;
	}
	
	public boolean getSpecial()
	{
		return filmSpecial;
	}
	
	public String getThumbnail()
	{
		return filmThumbnail;
	}
	
}

class FilmList
{

	Film[] fList;
	
	public void createList()
	{
	
	   fList = new Film[10];
		fList[0] = new Film("Austin Powers in Goldmember",true,true,false,"apowers3.jpg");
		fList[1] = new Film("Enemy At the Gates",true,false,true,"enemyatthegates.jpg");
		fList[2] = new Film("Fist of Fury",false,true,true,"fistoffury.jpg");
		fList[3] = new Film("Hearts in Atlantis",true,false,false,"heartsinatlantis.jpg");
		fList[4] = new Film("Judge Dredd",false,true,false,"judgedredd.jpg");
     
	   fList[5] = new Film("Star Trek Nemesis",true,true,false,"startreknemesis.jpg");
		fList[6] = new Film("Toy Story 2",true,true,false,"toystory2.jpg");
		fList[7] = new Film("Training Day",true,false,true,"trainingday.jpg");
		fList[8] = new Film("Twister",false,true,true,"twister.jpg");
		fList[9] = new Film("X Files",true,false,false,"xfiles.jpg");
	
	}
	
	public static void main(String[] args)
	{
		Film[] film = new Film[10];
		System.out.println(film.getTitle());
	}

}


This post has been edited by tvenhc: 30 March 2011 - 05:07 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Calling an array inside main function.. Urgent help

#2 sakshamkum  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 19
  • View blog
  • Posts: 232
  • Joined: 09-June 09

Re: Calling an array inside main function.. Urgent help

Posted 30 March 2011 - 05:25 AM

you can make an object of
FilmList class and then use the array



you cannot use it directly as it is a non static data member and main() can only refer static mambers directly

you can make an object of
FilmList class and then use the array



you cannot use it directly as it is a non static data member and main() can only refer static mambers directly
Was This Post Helpful? 1
  • +
  • -

#3 TFoSSDQ  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 123
  • View blog
  • Posts: 253
  • Joined: 09-December 10

Re: Calling an array inside main function.. Urgent help

Posted 30 March 2011 - 06:24 AM

Set your method to a static return method which returns a Film array:
public static Film[] createList(Film[] f) 


and return f later after loading it. You wouldn't need fList at that point.
main would be:
Film[] film = new Film[10];  
film = createList(film);
System.out.println(film[someIndex].getTitle());  


This post has been edited by TFoSSDQ: 30 March 2011 - 06:26 AM

Was This Post Helpful? 2
  • +
  • -

#4 Greltam  Icon User is offline

  • D.I.C Head

Reputation: 90
  • View blog
  • Posts: 225
  • Joined: 29-January 09

Re: Calling an array inside main function.. Urgent help

Posted 30 March 2011 - 09:33 AM

TFossDQ is correct. You need initialized objects in your
	Film[] film = new Film[10];

Array. This creates a new array of film with 10 slots. However, it does not actually create Film objects in those slots. You'd have to initialize each Film object like
for(Film f : film)
   f = new Film();


and then set the film properties if you wanted to System.out film[index].getTitle();
Was This Post Helpful? 1
  • +
  • -

#5 tvenhc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 30-March 11

Re: Calling an array inside main function.. Urgent help

Posted 30 March 2011 - 12:28 PM

Thank you all for your support!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1