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: Not saving object into Linked List

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Not saving object into Linked List

    So I am creating a Netflix based simple application, that allows you to add movies into a list and then create playlist, etc. This is the code below

    import java.io.IOException;
    import java.util.Date;
    import java.util.LinkedList;
    import java.util.Queue;
    import java.util.Scanner;
     
     
    public class UserInterface extends Users {
     
     
    	public UserInterface(String userName, double accountNumber,
    			Queue<Movie> playList, Queue<Movie> lastMovies) {
    		super(userName, accountNumber, playList, lastMovies);
    	}
     
    	static LinkedList<Movie> movieDatabase = new LinkedList<Movie>();
     
    	public static Scanner keyboard = new Scanner(System.in);
     
    	public static void main(String[] args){		
     
    		Scanner keyboard = new Scanner(System.in);
    		while(true){
     
    			System.out.println("X) Exit");
    			System.out.println("0) Create a new movie");
    			System.out.println("1) Add Movie to your playlist");
    			System.out.println("2) Upgrade your account");
     
    			char c = keyboard.next().charAt(0);
    			switch(c){
    				case 'X':
    					System.exit(0);
    				case '0':
    					createMovie();
    					break;
    				case '1':
    					for(int i= 0; i < movieDatabase.size(); i++)
    					{
    					System.out.println(movieDatabase);
    					}
    					break;
    				case '2':
    					System.out.println("Small one time free of $10,000,000!");
    					break;
    			}	
    		}
    	}
    	public static void createMovie()
    	{
     
    		String password = "adminpassword";
    		boolean isAdmin = false;
    		double inputPrice = 0;
    		String inputMovie;
    		String inputGenre;
    		Date inputDate = new Date(); 
    		String inputPassword;
     
    		System.out.println("Enter Movie Title:");
    		inputMovie = keyboard.nextLine();
    		System.out.println("Enter Movie Genre:");
    		inputGenre = keyboard.nextLine();
    		System.out.println("Enter Admin Password Twice:");
    		inputPassword = keyboard.nextLine();
    		passCheck(inputPassword, password, isAdmin);
    		adminPower(isAdmin, inputPrice);
    		Movie newMovie = new Movie(inputMovie, inputDate, inputGenre, inputPrice);
    		movieDatabase.add(newMovie);
     
    	}
     
    	    public static void passCheck(String inputPassword, String password, boolean isAdmin)
    	    {
    	    if(keyboard.next().equals("adminpassword"))
    	    {
    	    	isAdmin = true;
    	    	System.out.println("Welcome Administrator");
    	    	return;
    	    }
    	    else System.out.println("Sorry wrong password");
    	    }
     
    	    public static void adminPower(boolean isAdmin, double inputPrice)
    	    {
    	    	isAdmin = true;
    		if (isAdmin == true)
    		{
    			System.out.println("Enter Movie Price:");
    		    inputPrice = keyboard.nextDouble();
    		}
    		else
    		return;
    	    }
     
    	}

    Now where my error is that it won't save the "newMovie" object into the Movie linked list "movieDatabase" it just prints it out as nulls.


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Not saving object into Linked List

    for(int i= 0; i < movieDatabase.size(); i++)
    {
        System.out.println(movieDatabase);
    }

    If that loop is supposed to print the database contents, then you might want to think again. What it does is print movieDatabase a whole lot of times. What you should do is get each Movie from the database and print that.

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Not saving object into Linked List

    Well it is still returning everything as Null which means it isn't saving the element in the first place. How would i go about fixing this?

Similar Threads

  1. Replies: 1
    Last Post: October 25th, 2012, 02:03 PM
  2. Saving string value of an iterator Object into a variable.
    By apocalipsis1982 in forum What's Wrong With My Code?
    Replies: 16
    Last Post: December 28th, 2011, 01:12 PM
  3. Linked list Schminked list help with Nodes Please
    By Bially in forum Collections and Generics
    Replies: 1
    Last Post: September 29th, 2011, 03:20 PM
  4. Saving session info in a context object
    By bhask1 in forum Web Frameworks
    Replies: 0
    Last Post: September 19th, 2011, 02:14 PM
  5. saving list of data from JSP into java object
    By nrao in forum JavaServer Pages: JSP & JSTL
    Replies: 4
    Last Post: January 12th, 2011, 11:24 AM

Tags for this Thread