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 5 of 5

Thread: Java - Going through value in multiple ArrayList (Nested for loop?)

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java - Going through value in multiple ArrayList (Nested for loop?)

    Hi, I just started playing around with Java for awhile, but got caught up in a problem when using ArrayList.
    Edit: sorry about that, here's the full overview of it.

    CinemaAppMain
    public class CinemaAppMain {
      public static void main(String[] args) {
        new CinemaApp().start();
      }
    }

    CinemaApp
    import java.util.ArrayList;
    import java.util.Date;
     
    public class CinemaApp {
     
     
    	private ArrayList<Movie> movies = new ArrayList<Movie>();
    	private ArrayList<Theatre> theatres = new ArrayList<Theatre>();
    	private ArrayList<MovieScreening> screenings = new ArrayList<MovieScreening>();
    	private ArrayList<Customer> customers = new ArrayList<Customer>();
    	private ArrayList<Review> reviews = new ArrayList<Review>();
     
        public void start() {
     
    	String[] menu = { "Add New Movie", "Add New Theatre", "Add New Movie Screening","Display All Movie Screenings" };
     
    	String title = "Sim Bing Chen's Cinema Application";
    	while (true) {
    	    int choice = Helper.getUserOption(title, menu);
    	    if (choice == 1) {
    		addMovie();
    	    } else if (choice == 2) {
    		addTheatre();
    	    } else if (choice == 3) {
    		addScreening();
    	    } else if (choice == 4) {
    		registerCustomer();
    	    } else if (choice == 5) {
    		postReview();
    	    } else if (choice == 6){
    	    displayAllMovies();
    	    } else if (choice == 7){
    	    	displayAllTheatres();
    	    } else if (choice == 8){
    	    	displayAllScreenings();
    	    } else if (choice == 9){
    	    	displayAllCustomers();
    	    } else if (choice == 10){
    	    	displayAllReviews();
    	    } else if (choice == 11){
    	    	bookMovieTicket();
    	    } else if (choice == 12){
    	    	searchReviewsByMovie();
    		break;
    	    }
    	}
    	System.out.println("End of Application");
     
        }
     
        public void addMovie() {
        	System.out.println("- ADD NEW MOVIE -");
        	String title = new String(); // user input
        	String cast1 = new String(); // user input
        	String cast2 = new String(); // user input
        	String[] cast = new String[]{cast1,cast2}; // user input
        	String director = new String(); // user input
        	String genre = new String(); // user input
        	String language = new String(); // user input
        	Date date =  new Date(); // user input
     
        	movies.add(new Movie(title,cast,director,genre,language,date));
        	System.out.println("Added successfully");
        }
     
        public void addTheatre() {
    	System.out.println("-ADD NEW THEATRE-");
    	String name = new String(); // user input
    	String description =  new String(); // user input
    	int capacity =  new int(); // user input
     
    	theatres.add(new Theatre(name,description,capacity));
    	System.out.println("Added successfully");
     
        }
     
     
    	public void addScreening() {
        	System.out.println("-ADD NEW SCREENING-");
     
        	String mTitle =  new String(); // user input
        	String tName =  new String(); // user input
     
     
        	 	for (int i=0;i<movies.size();i++){
        		for(int j=0;j<theatres.size();j++){
        		if((movies.get(i).getTitle().contains(mTitle) && movies.get(i).getTitle() != null) || (theatres.get(i).getName().contains(tName) && theatres.get(i).getName() != null)){
        			int year = new int(); // user input
            		int month = new int(); // user input
            		int day = new int(); // user input
            		int hour = new int(); // user input
            		int min = new int(); // user input
     
                	screenings.add(new MovieScreening(Helper.thisDate(year, month, day, hour, min),movies.get(i),theatres.get(i),0));
            		System.out.println("Added successfully");
     
        		}else if((!movies.get(i).getTitle().contains(mTitle) && movies.get(i).getTitle() != null) || (!theatres.get(i).getName().contains(tName) && theatres.get(i).getName() != null)){
        			System.out.println("Your movie or/and theatre cannot be found.");
        		}
        		}
        	}
    	}
     
    public void displayAllScreenings() {
    	System.out.println("-All Movie Screenings-");
     
    	for (int i = 0; i < screenings.size(); i++) {
    	    System.out.print("[" + (i + 1) + "] ");
    	    screenings.get(i).display();
    	    Helper.line(45, ".");
    	}
        }

    MovieScreening
    import java.text.SimpleDateFormat;
    import java.util.Date;
     
    public class MovieScreening {
        private Date dateTime;
        private Movie movieObject;
        private Theatre theatreObject;
        private int bookings;
     
        public MovieScreening(Date dateTime,Movie movieObject,
    	    Theatre theatreObject, int bookings) {
    	this.dateTime = dateTime;
    	this.movieObject = movieObject;
    	this.theatreObject = theatreObject;
    	this.bookings = bookings;
        }
     
     
    	public void display() {
    	SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm");
    	System.out.println("Movie Screening Date & Time : " + df.format(dateTime));
    	System.out.println("Movie Title : " + movieObject.getTitle());
    	System.out.println("Theatre Name : " + theatreObject.getName());
    	System.out.println("Bookings : " + bookings);
        }
    	public int getBookings() {
    		return bookings;
    	}
     
    	public void setBookings(int bookings) {
    		this.bookings = bookings;
    	}
     
    	public Movie getMovieObject() {
    		return movieObject;
    	}
     
    	public Theatre getTheatreObject() {
    		return theatreObject;
    	}
    }

    I am trying to get the movie name and theatre title from their ArrayList, and I know I need to go through the movie list & theatre list to find their name & title, and compare it with the user input and print out the result.

    I use for(int i=0;i<movies.size();i++) to go through the the movie list, and I tried using for(int i=0;i<theatres.size();i++) to go through the theatre list. But when I printing multiple time with different user input value, it will show me strange result (screening have same movie name & theatre name, the else if statement is printed below the user input, user input is printed more than once after the first time).

    Help please!

    Edit: I know its wrong for me to use nested for loop,if not, another solution is needed to get the item inside both of the list.
    (getting the "title" from movie list & "name" from theatre list)

    If any one got a solution to get them out, I will appreciate it.
    Last edited by KirinXHell; January 24th, 2014 at 08:56 AM. Reason: Add in full code


  2. #2
    Junior Member
    Join Date
    Jan 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Eclipse - Going through value in multiple ArrayList (Nested for loop?)

    Hi, I just started playing around with Java for awhile, but got caught up in a problem when using ArrayList.

    for (int i=0;i<movies.size();i++){
        		for(int j=0;j<theatres.size();j++){
        		if((movies.get(i).getTitle().contains(mTitle) && movies.get(i).getTitle() != null) || (theatres.get(j).getName().contains(tName) && theatres.get(j).getName() != null)){
        			int year = new int(); // User input
            		int month = new int(); // User input
            		int day = new int(); // User input
            		int hour = new int(); // User input
            		int min = new int(); // User input
     
                	screenings.add(new MovieScreening(Helper.thisDate(year, month, day, hour, min),movies.get(i),theatres.get(j),0));
            		System.out.println("Added successfully");
     
        		}else if((!movies.get(i).getTitle().contains(mTitle) && movies.get(i).getTitle() != null) || (!theatres.get(j).getName().contains(tName) && theatres.get(j).getName() != null)){
        			System.out.println("Your movie or/and theatre cannot be found.");
        		}		
        		}
        	}

    I am trying to get the movie name and theatre title from their ArrayList, and I know I need to go through the movie list & theatre list to find their name & title, and compare it with the user input and print out the result.

    I use for(int i=0;i<movies.size();i++) to go through the the movie list, and I tried using for(int i=0;i<theatres.size();i++) to go through the theatre list. But when I printing multiple time with different user input value, it will show me strange result (screening have same movie name & theatre name, the else if statement is printed below the user input, user input is printed more than once after the first time).

    I have tried many method like moving around the for loop and if else statement, but same problem occur.

    Help please!

  3. #3
    Member
    Join Date
    Oct 2013
    Location
    United Kingdom
    Posts
    62
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: Eclipse -

    Can we please have the complete code?

    --- Update ---

    I think the thread has been duplicated, please can you close any one of these.
    Thanks and regards,
    Sambit Swain

  4. #4
    Member
    Join Date
    Oct 2013
    Location
    United Kingdom
    Posts
    62
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: Eclipse - Going through value in multiple ArrayList (Nested for loop?)

    Please could you post the full code? Also please provide the input and output which you tried.
    Thanks and regards,
    Sambit Swain

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Eclipse - Going through value in multiple ArrayList (Nested for loop?)

    Duplicate topics merged.

    @KirinXHell: Welcome to the Forum! Please read this topic to see useful info for newcomers.

    Of special note, please do not start duplicate topics, and give your topics useful titles. The longer version used on this one is much better than "Eclipse -" on the other. Further, Eclipse is a fancy source code editor. You're programming in and asking for help with Java, not Eclipse. There's a difference.

    Thanks, and keep coding!

Similar Threads

  1. nested for loop
    By yuli in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 11th, 2013, 07:52 PM
  2. nested while loop
    By Amruta N in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 2nd, 2013, 12:35 AM
  3. Need help with this nested loop
    By beerye28 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 22nd, 2013, 09:51 PM
  4. Please Help: Need Help with my Nested Loop
    By hiroprotagonist in forum Loops & Control Statements
    Replies: 1
    Last Post: November 24th, 2012, 10:43 AM
  5. Please Help: Need Help with my Nested Loop
    By hiroprotagonist in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 20th, 2012, 03:49 PM