Page 1 of 2 12 LastLast
Results 1 to 20 of 23
Like Tree3Likes

Thread: Print Parts Of An Array [User Input]

  1. #1
    Join Date
    Jan 2011
    Posts
    27
    Rep Power
    0

    Default Print Parts Of An Array [User Input]

    Hi All,

    I am learning Java and am still quite new to it.
    I am going through a book (Head First Java) and have decided to go a bit further on an exercise.

    Its just a bit of fun, but a good learning one for me anyway!! :)

    I want to produce a Java Program that relies on user input, so that when they input a number from the array, it will print certain details. In this case, a Movie name, its Star and the Movie Plot.

    If I show you what I have done so far hopefully someone can help me.

    Many thanks!!

    Java Code:
    /**
     * Write a description of class Movies here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    import java.util.Scanner;
    
    class Movies
    {
        String title;
        String staring;
        String plot;
    }
    
    class MoviesTestDrive {
    
        public static void main (String[] args){
            
                Scanner in = new Scanner(System.in);
                
                System.out.print("Please enter a number between 1 and 5 for a Movie: ");
        
                Movies [] myMovies = new Movies[5]; //Declare the Array!!
                int x = 0;
                
                myMovies[0] = new Movies();
                myMovies[1] = new Movies();
                myMovies[2] = new Movies();
                myMovies[3] = new Movies();
                myMovies[4] = new Movies();
                
                myMovies[0].title = "Commando";
                myMovies[1].title = "Predator";
                myMovies[2].title = "The Terminator";
                myMovies[3].title = "Rambo";
                myMovies[4].title = "The Expendables";
                
                myMovies[0].staring = "Arnold Schwarzenegger";
                myMovies[1].staring = "Arnold Schwarzenegger";
                myMovies[2].staring = "Arnold Schwarzenegger";
                myMovies[3].staring = "Sylvester Stallone";
                myMovies[4].staring = "Sylvester Stallone";
                
                myMovies[0].plot = "A retired special agent named John Matrix led an elite unit and has left the armed forces to live in a secluded mountain home with his daughter Jenny. But now he is forced out of retirement when his daughter is kidnapped by a band of thugs intent on revenge! Unbeknownst to Matrix, the members of his former unit are being killed one by one. Even though Matrix' friend General Franklin Kirby gives Matrix armed guards, attackers manage to kidnap Matrix and Jenny. Matrix learns that Bennett, a former member of his Matrix' unit who was presumed dead has kidnapped him to try to force Matrix to do a political assassination for a man called Arius (who calls himself El Presidente), a warlord formerly bested by Matrix who wishes to lead a military coup in his home country. Since Arius will have Jenny killed if Matrix refuses, Matrix reluctantly accepts the demand.";
                myMovies[1].plot = "A team of special force ops, led by a tough but fair soldier, Major 'Dutch' Schaefer, are ordered in to assist CIA man, George Dillon, on a rescue mission for potential survivors of a Helicopter downed over remote South American jungle. Not long after they land, Dutch and his team discover that they have been sent in under false pretenses. This deception turns out to be the least of their worries though, when they find themselves being methodically hunted by something not of this world.";
                myMovies[2].plot = "A cyborg is sent from the future on a deadly mission. He has to kill Sarah Connor, a young woman whose life will have a great significance in years to come. Sarah has only one protector - Kyle Reese - also sent from the future. The Terminator uses his exceptional intelligence and strength to find Sarah, but is there any way to stop the seemingly indestructible cyborg ?";
                myMovies[3].plot = "Vietnam veteran John Rambo has survived many harrowing ordeals in his lifetime and has since withdrawn into a simple and secluded existence in Thailand, where he spends his time capturing snakes for local entertainers, and chauffeuring locals in his old PT boat. Even though he is looking to avoid trouble, trouble has a way of finding him: a group of Christian human rights missionaries, led by Michael Burnett and Sarah Miller, approach Rambo with the desire to rent his boat to travel up the river to Burma. For over fifty years, Burma has been a war zone. The Karen people of the region, who consist of peasants and farmers, have endured brutally oppressive rule from the murderous Burmese military and have been struggling for survival every single day. After some inner contemplation, Rambo accepts the offer and takes Michael, Sarah, and the rest of the missionaries up the river. When the missionaries finally arrive at the Karen village...";
                myMovies[4].plot = "Barney Ross leads the 'Expendables', a band of highly skilled mercenaries including knife enthusiast Lee Christmas, martial arts expert Yin Yang, heavy weapons specialist Hale Caesar, demolitionist Toll Road and loose-cannon sniper Gunner Jensen. When the group is commissioned by the mysterious Mr. Church to assassinate the merciless dictator of a small South American island, Barney and Lee head to the remote locale to scout out their opposition. Once there, they meet with local rebel Sandra and discover the true nature of the conflict engulfing the city. When they escape the island and Sandra stays behind, Ross must choose to either walk away and save his own life - or attempt a suicidal rescue mission that might just save his soul.";
                
            }        
    }

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default

    I think you have have an ok start. Think about what you are trying to do. Java is Object Oriented, so you can model just about anything as an Object. I think it would make more sense to create a Movie object and that might help you understand Java more too. Think about a Movie. You know the movie has a plot, a star, a title, and in this case, we will give it an id. So here is what a Movie Object might look like:

    Java Code:
    public class Movie{
    String title;
    String starring;
    String plot;
    int id;
    
    public Movie(String title, String starring, String plot, int id){
    this.title = title;
    this.starring = starring;
    this.plot = plot;
    this.id = id;
    }
    
    public void setTitle(String title){
    this.title = title;
    }
    
    public void setPlot(String plot){
    this.plot = plot;
    }
    
    public void setStarring(String starring){
    this.starring = starring;
    }
    
    public void setId(int id){
    this.id = id;
    }
    
    public String getStarring(){
    return starring;
    }
    
    public String getTitle(){
    return title;
    }
    
    public String getPlot(){
    return plot;
    }
    
    public int getId(){
    return id;
    }
    
    }
    The first part of this code is the constructor:

    Java Code:
    public Movie(String title, String starring, String plot, int id){
    this.title = title;
    this.starring = starring;
    this.plot = plot;
    this.id = id;
    }
    The constructor helps you to create an instance of your Movie class. So, when you want to create a new instance of a Movie you would do so with:

    Java Code:
    Movie commando = new Movie("Commando", "Arnold Schwarzenegger", "The Plot of the Movie", 1);
    So what you have done with that 1 line of code is created a new instance of a Movie, and defined all 4 of those properties. So in your program, once you get back the id from the user that they enter, you can use your getters that are a part of the Movie class, and return any piece of data you want. So let's say they enter a 1 (for Commando), and you wan't to get back just the plot. You would call getPlot:

    Java Code:
    String theplot = commando.getPlot();
    So now you have a string called theplot that holds the plot of the movie, and you can print that out to the user.

    Does that make sense?
    Last edited by sehudson; 08-24-2011 at 10:54 PM.
    PrimalScientist likes this.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You are not using the variable x so get rid of it: variables should have descriptive names (like your myMovies) and should actually be used somewhere lest they be clutter.

    If I understand your problem, it boils down to

    (1) Get an index value from the user
    (2) Print things from the movie at that index value

    For (1) have a look at the Scanner methods for something that will return you an int value to be used as the index.

    Once you have that value (2) is easy:

    Java Code:
    System.out.println("The title is " + myMovies[index].title);
    // etc
    [Edit] These suggestions are independent of whether you go for an object oriented approach. (Something which looks better and better as you develop your program along the lines of multiple searches, data held outside the program which you might want to edit etc). If you do go for using a Movie class, then make the instance variables private unless you have a really good reason not to. Also think about what you want your class to be (or do: often we think of them in terms of their behaviour). For instance does a movie change its title? If not, then a setTitle() method is not needed.
    Last edited by pbrockway2; 08-24-2011 at 10:49 PM.
    PrimalScientist likes this.

  4. #4
    Join Date
    Jan 2011
    Posts
    27
    Rep Power
    0

    Default

    It does I think, and that was a great reply to my thread actually, so thank you!!
    I have never used this.title and this.plot

    What does "this" actually do? Is it like a pointer if you like?

    Also, by doing that, I get the user to enter a number that will point to the id in my array.
    So, by producing another object, in this case perhaps an if, if else depending on what number is entered will display what movie?

    Thanks again!!

  5. #5
    Join Date
    Jan 2011
    Posts
    27
    Rep Power
    0

    Default

    Thank you for this. Both replies are first class. I am enjoying it, and just finding my way to be honest.
    Been doing this at Uni, but I want to build on this now as its just good fun!!

  6. #6
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default

    'this' is a reference to the current object. If I had done something like this:

    public void setTitle(String titleInput){
    title = titleInput
    }
    The this keyword is not necessary in this case. It was only needed in the example I posted because the argument had the same name as the variable that I was setting it to. I cant have:

    Java Code:
    public void setTitle(String title){
    title = title;
    }
    That is considered an assignment to self, so the this keyword is a way of saying I am referencing the title of the object.

  7. #7
    Join Date
    Jan 2011
    Posts
    27
    Rep Power
    0

    Default

    Thanks for those replies there. Very, very helpful.
    The plot in each movie is very long. How can I return a couple of the lines so that the plot is not just one long line?
    For instance, in XHTML, to do a line return I would enter </br> in to my code where I want to start a new line.

    Cheers all!! :)

  8. #8
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default

    use \n to add a linefeed.

  9. #9
    Join Date
    Jan 2011
    Posts
    27
    Rep Power
    0

    Default

    Hey All,

    Okay, have hit a slight problem in that the System Input Scanner id does not like me using the "id" for user input?

    Here is my code so far...

    Java Code:
    /**
     * Write a description of class Movies here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    import java.util.Scanner;
    
    class Movies
    {
        String title;
        String starring;
        String plot;
        int id;
    
     
    
        public void Movie (String title, String starring, String plot, int id) {
            
                this.title = title;
                this.starring = starring;
                this.plot = plot;
                this.id = id;
            }
            
        public void setTitle(String title){
                this.title = title;
            }
            
        public void setStarring(String starring){
                this.starring = starring;
            }
            
        public void setPlot(String plot){
                this.plot = plot;
            }
            
        public void setId(int id){
                this.id = id;
            }
            
        public String getTitle(){
                return title;
            }
            
        public String getStarring(){
                return starring;
            }
            
        public String getPlot(){
                return plot;
            }
            
        public int getId(){
                return id;
            }
                
        public static void main(String [] args){
            
                Scanner in = new Scanner(System.in); //Looking for user input.
                
                System.out.print("Please enter a number between 1 and 5 for a Movie: ");
        
                id = in.nextInt(); //JAVA DOES NOT SEEM TO LIKE THIS???
                
                Movie commando = new Movie("Commando","Arnold Schwarzenegger","A retired special agent named John Matrix led an elite unit and (has left the armed forces to live in a secluded mountain home with his daughter Jenny. But now he is forced out of retirement when his daughter is kidnapped by a band of thugs intent on revenge! Unbeknownst to Matrix, the members of his former unit are being killed one by one. Even though Matrix' friend General Franklin Kirby gives Matrix armed guards, attackers manage to kidnap Matrix and Jenny. Matrix learns that Bennett, a former member of his Matrix' unit who was presumed dead has kidnapped him to try to force Matrix to do a political assassination for a man called Arius (who calls himself El Presidente), a warlord formerly bested by Matrix who wishes to lead a military coup in his home country. Since Arius will have Jenny killed if Matrix refuses, Matrix reluctantly accepts the demand.", 1);
                Movie predator = new Movie("Predator","Arnold Schwarzenegger","A team of special force ops, led by a tough but fair soldier, Major 'Dutch' Schaefer, are ordered in to assist CIA man, George Dillon, on a rescue mission for potential survivors of a Helicopter downed over remote South American jungle. Not long after they land, Dutch and his team discover that they have been sent in under false pretenses. This deception turns out to be the least of their worries though, when they find themselves being methodically hunted by something not of this world.", 2);
                Movie terminator = new Movie("The Terminator","Arnold Schwarzenegger","A cyborg is sent from the future on a deadly mission. He has to kill Sarah Connor, a young woman whose life will have a great significance in years to come. Sarah has only one protector - Kyle Reese - also sent from the future. The Terminator uses his exceptional intelligence and strength to find Sarah, but is there any way to stop the seemingly indestructible cyborg ?", 3);
                Movie rambo = new Movie("Rambo","Sylvester Stallone","Vietnam veteran John Rambo has survived many harrowing ordeals in his lifetime and has since withdrawn into a simple and secluded existence in Thailand, where he spends his time capturing snakes for local entertainers, and chauffeuring locals in his old PT boat. Even though he is looking to avoid trouble, trouble has a way of finding him: a group of Christian human rights missionaries, led by Michael Burnett and Sarah Miller, approach Rambo with the desire to rent his boat to travel up the river to Burma. For over fifty years, Burma has been a war zone. The Karen people of the region, who consist of peasants and farmers, have endured brutally oppressive rule from the murderous Burmese military and have been struggling for survival every single day. After some inner contemplation, Rambo accepts the offer and takes Michael, Sarah, and the rest of the missionaries up the river. When the missionaries finally arrive at the Karen village...", 4);
                Movie expendables = new Movie("The Expendables","Sylvester Stallone","Barney Ross leads the 'Expendables', a band of highly skilled mercenaries including knife enthusiast Lee Christmas, martial arts expert Yin Yang, heavy weapons specialist Hale Caesar, demolitionist Toll Road and loose-cannon sniper Gunner Jensen. When the group is commissioned by the mysterious Mr. Church to assassinate the merciless dictator of a small South American island, Barney and Lee head to the remote locale to scout out their opposition. Once there, they meet with local rebel Sandra and discover the true nature of the conflict engulfing the city. When they escape the island and Sandra stays behind, Ross must choose to either walk away and save his own life - or attempt a suicidal rescue mission that might just save his soul.", 5);
                
                
                
                
                
                    
                    
                }
                
            }
    Am I heading in the right direction here?

    Regards,

    Daz.

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You have not declared the variable id anywhere.

    Notice this variable (whose value comes from the user) is not the same as the id variable used by the Movie instances which each have their own value for their id. It might be a good idea to use a different name for the variable - one that is descriptive of its intended purpose.

  11. #11
    Join Date
    Jan 2011
    Posts
    27
    Rep Power
    0

    Default

    Okay, been looking at this...

    The compiler cannot find the Class Movies??? How do you get it to find this?
    It is in the code!!

    Java Code:
    /**
     * Write a description of class Movies here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    import java.util.Scanner;
    
    class Movies
    {
        String title;
        String starring;
        String plot;
        int id;
    
     
    
        public void Movie (String title, String starring, String plot, int id) {
            
                this.title = title;
                this.starring = starring;
                this.plot = plot;
                this.id = id;
            }
            
        public void setTitle(String title){
                this.title = title;
            }
            
        public void setStarring(String starring){
                this.starring = starring;
            }
            
        public void setPlot(String plot){
                this.plot = plot;
            }
            
        public void setId(int id){
                this.id = id;
            }
            
        public String getTitle(){
                return title;
            }
            
        public String getStarring(){
                return starring;
            }
            
        public String getPlot(){
                return plot;
            }
            
        public int getId(){
                return id;
            }
                
        public static void main(String [] args){
            
                int numberId;
                Scanner in = new Scanner(System.in); //Looking for user input.
                
                System.out.print("Please enter a number between 1 and 5 for a Movie: ");
        
                numberId = in.nextInt();
                
                Movie commando = new Movie("Commando","Arnold Schwarzenegger","A retired special agent named John Matrix led an elite unit and (has left the armed forces to live in a secluded mountain home with his daughter Jenny. But now he is forced out of retirement when his daughter is kidnapped by a band of thugs intent on revenge! Unbeknownst to Matrix, the members of his former unit are being killed one by one. Even though Matrix' friend General Franklin Kirby gives Matrix armed guards, attackers manage to kidnap Matrix and Jenny. Matrix learns that Bennett, a former member of his Matrix' unit who was presumed dead has kidnapped him to try to force Matrix to do a political assassination for a man called Arius (who calls himself El Presidente), a warlord formerly bested by Matrix who wishes to lead a military coup in his home country. Since Arius will have Jenny killed if Matrix refuses, Matrix reluctantly accepts the demand.", 1);
                Movie predator = new Movie("Predator","Arnold Schwarzenegger","A team of special force ops, led by a tough but fair soldier, Major 'Dutch' Schaefer, are ordered in to assist CIA man, George Dillon, on a rescue mission for potential survivors of a Helicopter downed over remote South American jungle. Not long after they land, Dutch and his team discover that they have been sent in under false pretenses. This deception turns out to be the least of their worries though, when they find themselves being methodically hunted by something not of this world.", 2);
                Movie terminator = new Movie("The Terminator","Arnold Schwarzenegger","A cyborg is sent from the future on a deadly mission. He has to kill Sarah Connor, a young woman whose life will have a great significance in years to come. Sarah has only one protector - Kyle Reese - also sent from the future. The Terminator uses his exceptional intelligence and strength to find Sarah, but is there any way to stop the seemingly indestructible cyborg ?", 3);
                Movie rambo = new Movie("Rambo","Sylvester Stallone","Vietnam veteran John Rambo has survived many harrowing ordeals in his lifetime and has since withdrawn into a simple and secluded existence in Thailand, where he spends his time capturing snakes for local entertainers, and chauffeuring locals in his old PT boat. Even though he is looking to avoid trouble, trouble has a way of finding him: a group of Christian human rights missionaries, led by Michael Burnett and Sarah Miller, approach Rambo with the desire to rent his boat to travel up the river to Burma. For over fifty years, Burma has been a war zone. The Karen people of the region, who consist of peasants and farmers, have endured brutally oppressive rule from the murderous Burmese military and have been struggling for survival every single day. After some inner contemplation, Rambo accepts the offer and takes Michael, Sarah, and the rest of the missionaries up the river. When the missionaries finally arrive at the Karen village...", 4);
                Movie expendables = new Movie("The Expendables","Sylvester Stallone","Barney Ross leads the 'Expendables', a band of highly skilled mercenaries including knife enthusiast Lee Christmas, martial arts expert Yin Yang, heavy weapons specialist Hale Caesar, demolitionist Toll Road and loose-cannon sniper Gunner Jensen. When the group is commissioned by the mysterious Mr. Church to assassinate the merciless dictator of a small South American island, Barney and Lee head to the remote locale to scout out their opposition. Once there, they meet with local rebel Sandra and discover the true nature of the conflict engulfing the city. When they escape the island and Sandra stays behind, Ross must choose to either walk away and save his own life - or attempt a suicidal rescue mission that might just save his soul.", 5);
                
                
                
                
                
                    
                    
                }
                
            }

  12. #12
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default

    Compile it and correct all errors that compiler shows. for example your constructor...

  13. #13
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Or post the errors if you can't understand them.

    -----

    Note, too, that "Movie" is not "Movies".

  14. #14
    Join Date
    Jan 2011
    Posts
    27
    Rep Power
    0

    Default

    The error I get is:

    Cannot find symbol - Constructor Movies (java.lang.String.java.lang.String.java.lang.Strin g.int)

    Java Code:
    /**
     * Write a description of class Movies here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    import java.util.Scanner;
    
    public class Movies
    {
        String title;
        String starring;
        String plot;
        int id;
    
     
    
        public void Movies (String title, String starring, String plot, int id) {
            
                this.title = title;
                this.starring = starring;
                this.plot = plot;
                this.id = id;
            }
            
        private void setTitle(String title){
                this.title = title;
            }
            
        private void setStarring(String starring){
                this.starring = starring;
            }
            
        private void setPlot(String plot){
                this.plot = plot;
            }
            
        private void setId(int id){
                this.id = id;
            }
            
        private String getTitle(){
                return title;
            }
            
        private String getStarring(){
                return starring;
            }
            
        private String getPlot(){
                return plot;
            }
            
        private int getId(){
                return id;
            }
                
        public static void main (String [] args){
            
                int numberId;
                
                Scanner in = new Scanner(System.in); //Looking for user input.
                
                System.out.print("Please enter a number between 1 and 5 for a Movie: ");
        
                numberId = in.nextInt();
                
                
                
                Movies commando = new Movies("Commando","Arnold Schwarzenegger","A retired special agent named John Matrix led an elite unit and (has left the armed forces to live in a secluded mountain home with his daughter Jenny. But now he is forced out of retirement when his daughter is kidnapped by a band of thugs intent on revenge! Unbeknownst to Matrix, the members of his former unit are being killed one by one. Even though Matrix' friend General Franklin Kirby gives Matrix armed guards, attackers manage to kidnap Matrix and Jenny. Matrix learns that Bennett, a former member of his Matrix' unit who was presumed dead has kidnapped him to try to force Matrix to do a political assassination for a man called Arius (who calls himself El Presidente), a warlord formerly bested by Matrix who wishes to lead a military coup in his home country. Since Arius will have Jenny killed if Matrix refuses, Matrix reluctantly accepts the demand.", 1);
                Movie predator = new Movie("Predator","Arnold Schwarzenegger","A team of special force ops, led by a tough but fair soldier, Major 'Dutch' Schaefer, are ordered in to assist CIA man, George Dillon, on a rescue mission for potential survivors of a Helicopter downed over remote South American jungle. Not long after they land, Dutch and his team discover that they have been sent in under false pretenses. This deception turns out to be the least of their worries though, when they find themselves being methodically hunted by something not of this world.", 2);
                Movie terminator = new Movie("The Terminator","Arnold Schwarzenegger","A cyborg is sent from the future on a deadly mission. He has to kill Sarah Connor, a young woman whose life will have a great significance in years to come. Sarah has only one protector - Kyle Reese - also sent from the future. The Terminator uses his exceptional intelligence and strength to find Sarah, but is there any way to stop the seemingly indestructible cyborg ?", 3);
                Movie rambo = new Movie("Rambo","Sylvester Stallone","Vietnam veteran John Rambo has survived many harrowing ordeals in his lifetime and has since withdrawn into a simple and secluded existence in Thailand, where he spends his time capturing snakes for local entertainers, and chauffeuring locals in his old PT boat. Even though he is looking to avoid trouble, trouble has a way of finding him: a group of Christian human rights missionaries, led by Michael Burnett and Sarah Miller, approach Rambo with the desire to rent his boat to travel up the river to Burma. For over fifty years, Burma has been a war zone. The Karen people of the region, who consist of peasants and farmers, have endured brutally oppressive rule from the murderous Burmese military and have been struggling for survival every single day. After some inner contemplation, Rambo accepts the offer and takes Michael, Sarah, and the rest of the missionaries up the river. When the missionaries finally arrive at the Karen village...", 4);
                Movie expendables = new Movie("The Expendables","Sylvester Stallone","Barney Ross leads the 'Expendables', a band of highly skilled mercenaries including knife enthusiast Lee Christmas, martial arts expert Yin Yang, heavy weapons specialist Hale Caesar, demolitionist Toll Road and loose-cannon sniper Gunner Jensen. When the group is commissioned by the mysterious Mr. Church to assassinate the merciless dictator of a small South American island, Barney and Lee head to the remote locale to scout out their opposition. Once there, they meet with local rebel Sandra and discover the true nature of the conflict engulfing the city. When they escape the island and Sandra stays behind, Ross must choose to either walk away and save his own life - or attempt a suicidal rescue mission that might just save his soul.", 5);
                
                System.out.print(numberId);
                
                
                
                
                    
                    
                }
                
            }

  15. #15
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default

    A constructor doesn't have a return type. What you're probably intending to be a constructor, isn't. It's a method.

    db

  16. #16
    Join Date
    Jan 2011
    Posts
    27
    Rep Power
    0

    Default

    Can you help me amend it please? I am a real newbie you see.

    Daz.

  17. #17
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default

    You already have the answer in this thread. Read all the responses -- carefully.

    db

  18. #18
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by PrimalScientist View Post
    I am going through a book (Head First Java) and have decided to go a bit further on an exercise.
    This is ok, but make sure you have understood the basics, that is creating classes, constructors and so on. So read this, reread the related chapters in your head first book and all responses given here in this thread.
    Last edited by j2me64; 08-29-2011 at 02:15 PM.
    PrimalScientist likes this.

  19. #19
    Join Date
    Jan 2011
    Posts
    27
    Rep Power
    0

    Default

    Hi All, I have nailed it finally. The CONSTRUCTOR MUST BE THE SAME AS THE CLASS!!

    I need to print out now from user input...
    Any tips? PLEASE? Thanks for being patient with me.

    Daz.

    Java Code:
    /**
     * Write a description of class Movies here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    import java.util.Scanner;
    
    public class Movies
    {
        String title;
        String starring;
        String plot;
        int id;
    
     
    
        public Movies (String title, String starring, String plot, int id) {
      
        }
            
        private void setTitle(String title){
                this.title = title;
            }
            
        private void setStarring(String starring){
                this.starring = starring;
            }
            
        private void setPlot(String plot){
                this.plot = plot;
            }
            
        private void setId(int id){
                this.id = id;
            }
            
        private String getTitle(){
                return title;
            }
            
        private String getStarring(){
                return starring;
            }
            
        private String getPlot(){
                return plot;
            }
            
        private int getId(){
                return id;
            }
                
        public static void main (String [] args) {
            
                int id;
                
                Scanner in = new Scanner(System.in); //Looking for user input.
                
                System.out.print("Please enter a number between 1 and 5 for a Movie: ");
        
                id = in.nextInt();
                
                Movies commando = new Movies("Commando","Arnold Schwarzenegger","A retired special agent named John Matrix led an elite unit and (has left the armed forces to live in a secluded mountain home with his daughter Jenny. But now he is forced out of retirement when his daughter is kidnapped by a band of thugs intent on revenge! Unbeknownst to Matrix, the members of his former unit are being killed one by one. Even though Matrix' friend General Franklin Kirby gives Matrix armed guards, attackers manage to kidnap Matrix and Jenny. Matrix learns that Bennett, a former member of his Matrix' unit who was presumed dead has kidnapped him to try to force Matrix to do a political assassination for a man called Arius (who calls himself El Presidente), a warlord formerly bested by Matrix who wishes to lead a military coup in his home country. Since Arius will have Jenny killed if Matrix refuses, Matrix reluctantly accepts the demand.", 1);
                Movies predator = new Movies("Predator","Arnold Schwarzenegger","A team of special force ops, led by a tough but fair soldier, Major 'Dutch' Schaefer, are ordered in to assist CIA man, George Dillon, on a rescue mission for potential survivors of a Helicopter downed over remote South American jungle. Not long after they land, Dutch and his team discover that they have been sent in under false pretenses. This deception turns out to be the least of their worries though, when they find themselves being methodically hunted by something not of this world.", 2);
                Movies terminator = new Movies("The Terminator","Arnold Schwarzenegger","A cyborg is sent from the future on a deadly mission. He has to kill Sarah Connor, a young woman whose life will have a great significance in years to come. Sarah has only one protector - Kyle Reese - also sent from the future. The Terminator uses his exceptional intelligence and strength to find Sarah, but is there any way to stop the seemingly indestructible cyborg ?", 3);
                Movies rambo = new Movies("Rambo","Sylvester Stallone","Vietnam veteran John Rambo has survived many harrowing ordeals in his lifetime and has since withdrawn into a simple and secluded existence in Thailand, where he spends his time capturing snakes for local entertainers, and chauffeuring locals in his old PT boat. Even though he is looking to avoid trouble, trouble has a way of finding him: a group of Christian human rights missionaries, led by Michael Burnett and Sarah Miller, approach Rambo with the desire to rent his boat to travel up the river to Burma. For over fifty years, Burma has been a war zone. The Karen people of the region, who consist of peasants and farmers, have endured brutally oppressive rule from the murderous Burmese military and have been struggling for survival every single day. After some inner contemplation, Rambo accepts the offer and takes Michael, Sarah, and the rest of the missionaries up the river. When the missionaries finally arrive at the Karen village...", 4);
                Movies expendables = new Movies("The Expendables","Sylvester Stallone","Barney Ross leads the 'Expendables', a band of highly skilled mercenaries including knife enthusiast Lee Christmas, martial arts expert Yin Yang, heavy weapons specialist Hale Caesar, demolitionist Toll Road and loose-cannon sniper Gunner Jensen. When the group is commissioned by the mysterious Mr. Church to assassinate the merciless dictator of a small South American island, Barney and Lee head to the remote locale to scout out their opposition. Once there, they meet with local rebel Sandra and discover the true nature of the conflict engulfing the city. When they escape the island and Sandra stays behind, Ross must choose to either walk away and save his own life - or attempt a suicidal rescue mission that might just save his soul.", 5);
                
                System.out.println(id);
            }
            
            
                
            }

  20. #20
    Join Date
    Jan 2011
    Posts
    27
    Rep Power
    0

Page 1 of 2 12 LastLast

Similar Threads

  1. user input array
    By localhost in forum New To Java
    Replies: 5
    Last Post: 12-30-2010, 04:00 AM
  2. Replies: 2
    Last Post: 12-02-2010, 04:18 AM
  3. Read user input into integer array
    By varunb in forum New To Java
    Replies: 12
    Last Post: 07-09-2010, 12:50 PM
  4. how to get input from User
    By Alvaro in forum New To Java
    Replies: 7
    Last Post: 01-15-2010, 11:02 PM
  5. Replies: 5
    Last Post: 11-26-2009, 07:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •