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: Printing Array Strings in a Method

  1. #1
    Junior Member
    Join Date
    Aug 2017
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Printing Array Strings in a Method

    Hi there, I am looking for direction on how to fix this code, as you can see I'm trying to print out the String and and enum which is stored in an array. The text however isn't printing

    This is my Main class:

    package test;
     
    import java.util.Scanner;
     
    public class Main {
     
    	public static void main(String[] args)
    	{
    		Movie m = new Movie();
     
    		Movie[] movies = new Movie[4];
     
    		m.chooseMovie();
     
    		m.movies[0]= new Movie("A", Rating.PG);
    		m.movies[1] = new Movie("B", Rating.PG);			
    		m.movies[2] = new Movie("C", Rating.M);			
    		m.movies[3] = new Movie("D", Rating.G);

    This is my movie class:

    package test;
     
    import java.util.Scanner;
     
    public class Movie
    {
    	String title;						
    	Rating rating;
    	int choice;
    	Movie[] movies;
     
    	public Movie(String title, Rating rating)		
    	{
    		this.title = title;
    		this.rating = rating;
    	}
     
     
    	public Movie chooseMovie()
    	{
    		Scanner scanner = new Scanner(System.in);
     
    		System.out.println("Which movie would you like to watch:");
    		System.out.println("1 "+movie[0]);
    		System.out.println("2 "+movie[1]);			
    		System.out.println("3 "+movie[2]);
    		System.out.println("4 "+movie[3]);
     
    		this.choice = scanner.nextInt();
     
    		return Movie.this;
    	}
     
     
    }


    Thanks in advance

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,382
    Thanks
    53
    Thanked 2,398 Times in 2,351 Posts

    Default Re: Printing Array Strings in a Method

    Can you post the program's current output to show what you are talking about?
    Also post an example of what you want the output to look like.

    Where is the enum's definition?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2017
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Printing Array Strings in a Method

    Quote Originally Posted by Norm View Post
    Can you post the program's current output to show what you are talking about?
    Also post an example of what you want the output to look like.

    Where is the enum's definition?
    Currently there is no output, there is an error with this line in the CinemaApp class:
    Movie m = new Movie();

    Ideally the output should look like:

    Which movie would you like to watch? 
    1 A (PG)
    2 B (PG)
    3 C (M)
    4 D (G)

    This is the enum's definition in it's own class:
    package test;
     
    public enum Rating {
     
    	G, PG, M
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,382
    Thanks
    53
    Thanked 2,398 Times in 2,351 Posts

    Default Re: Printing Array Strings in a Method

    there is no output, there is an error
    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jan 2014
    Posts
    2
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Printing Array Strings in a Method

    Alright let me thy this:

    In your main class you are making a reference:
    Movie[] movies = new Movie[4];
    But you don't use it.

    Then you call the
    chooseMovie()
    method but you on the 'm' reference. In this method you call the array objects as a String.
    Two point here:
    One: The array is in this state empty because AFTER you calling the chooseMovie method you add several movie objects to this array.
    Two: You want to print the objects as a String. You should make a method (in your Movie object) like 'toString' which will return an actual String.

    So in your choose movie Method it will look like this:
    System.out.println("1 "+movie[0].toString());

  6. The Following User Says Thank You to RvDelft For This Useful Post:

    caiz1 (August 9th, 2017)

Similar Threads

  1. Calling a print method from another class (printing array)
    By Kaldanis in forum Object Oriented Programming
    Replies: 7
    Last Post: November 25th, 2011, 12:32 PM
  2. Replies: 1
    Last Post: September 28th, 2011, 07:29 AM
  3. Array problems (printing strings)
    By James5955 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 12th, 2011, 12:21 AM
  4. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 02:41 AM