Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Array of Movies

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    7
    My Mood
    Depressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Array of Movies

    I cannot work out why both entries are the same: Also can I enter the information by using:

    movie(title, released, certificate, description, rating, genre)

    package test;
     
    public class TEST {
     
        public String movie_title="NA";
        public int movie_released=0000;
        public String movie_certificate="NA";
        public String movie_description="BLANK";
        public double movie_rating=0.0;
        public String movie_genre="MOVIE";  
     
        public static void main(String[] args) {
     
            int position = 0;
            TEST movie = new TEST();
            TEST MM[] = new TEST[2];
            movie.movie_title="TEST";
            movie.movie_released=1992;
            movie.movie_certificate="PG";
            movie.movie_description="Two people fall in love.";
            movie.movie_rating=10.0;
            movie.movie_genre="Chick Flick";
     
            MM[position]=movie;
            position++;
     
            movie.movie_title="Sneak and Stab";
            movie.movie_released=2001;
            movie.movie_certificate="18";
            movie.movie_description="Two people fall in hate.";
            movie.movie_rating=0.0;
            movie.movie_genre="Gore Flick";
     
            MM[position]=movie;
            //position++;
     
            int length = position;
     
            for(int Counter=0;Counter<=length;Counter++){
                System.out.println("\n\tMovie Record: " +Counter);
                System.out.println(MM[Counter].movie_title);
                System.out.println(MM[Counter].movie_released);
                System.out.println(MM[Counter].movie_certificate);
                System.out.println(MM[Counter].movie_description);
                System.out.println(MM[Counter].movie_rating);
                System.out.println(MM[Counter].movie_genre);
            }
     
        }
    }

    The output looks like this:

    	Movie Record: 0
    Sneak and Stab
    2001
    18
    Two people fall in hate.
    0.0
    Gore Flick
     
    	Movie Record: 1
    Sneak and Stab
    2001
    18
    Two people fall in hate.
    0.0
    Gore Flick


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Array of Movies

    Your movie variable is a single instance, since you only ever create a new TEST once. You're simply changing the values of that one instance and storing that one instance in two separate places in your array.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Dec 2011
    Location
    United States
    Posts
    94
    My Mood
    Amused
    Thanks
    5
    Thanked 8 Times in 8 Posts

    Default Re: Array of Movies

    Just for a good practice in Java: when creating your arrays, it is good to declare it with the brackets coming before the variable name. E.g int [] movies = {, , , , , };

Similar Threads

  1. Replies: 4
    Last Post: November 14th, 2011, 10:00 PM
  2. Doubling The Array Size And Randomizing Array Return
    By Pingu00 in forum What's Wrong With My Code?
    Replies: 18
    Last Post: June 27th, 2011, 10:50 AM
  3. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM
  4. Replies: 2
    Last Post: May 6th, 2011, 05:19 PM
  5. 2d (4x4) array insdie a 1d array. (Block cipher)
    By fortune2k in forum Collections and Generics
    Replies: 13
    Last Post: November 23rd, 2010, 05:29 PM