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: How to call a self created method from an object stored in an ArrayList ?

  1. #1
    Junior Member
    Join Date
    Sep 2018
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to call a self created method from an object stored in an ArrayList ?

    I'm a newbie, and I wholeheartedly admit that this should not be this hard.

    I have an ArrayList of objects representing Questions, Correct Answers, and 3 "Wrong" Answers.
    I have created a method named printIndividualQuestionItem() that prints out these items for each question, but I am unable to figure out how to call it from the main program.
    How can I call the printIndividualQuestionItem() method to be able to print out an individual question and the corresponding answers?
    Thank you in advance for any help.

    import java.util.*;
     
    public class Cpractice
    {
        public static void main(String[] args)
        {
            ArrayList<IndividualQuestionItem> bubba = new ArrayList<IndividualQuestionItem>();
     
            bubba.add( new IndividualQuestionItem("a", "b", "c", "d", "e") );
            bubba.add( new IndividualQuestionItem("2", "2", "2", "2", "2") );
     
            for (int i=0; i<bubba.size(); i++) 
                System.out.println((IndividualQuestionItem) bubba.get(i)); 
     
            System.out.println("Finished");
        }
    }
     
     
    class IndividualQuestionItem
    {
        private String Question = "";
        private String CorrectAnswer = "";
        private String WrongAnswerA = "";
        private String WrongAnswerB = "";
        private String WrongAnswerC = "";
     
        // Constructor for the IndividualQuestionItem class.
        // It takes 5 temporary variables to initialize the question items.
        public IndividualQuestionItem(String a, String b, String c, String d, String e)
        {
            Question = a;
            CorrectAnswer = b;
            WrongAnswerA = c;
            WrongAnswerB = d;
            WrongAnswerC = e;
        }
     
        // Print out the  for the IndividualQuestionItem class.
        //  public void printIndividualQuestionItem(IndividualQuestionItem temp)
        public void printIndividualQuestionItem()
        {
            System.out.print(this.Question + " ");
            System.out.print(this.CorrectAnswer + " ");
            System.out.print(this.WrongAnswerA + " ");
            System.out.print(this.WrongAnswerB + " ");
            System.out.println(this.WrongAnswerC);
            System.out.println();
        }
    }

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

    Default Re: How to call a self created method from an object stored in an ArrayList ?

    How can I call the printIndividualQuestionItem() method
    You need a reference to an instance of the class to be able to call its methods.
    To get at the contents of an ArrayList, call its get method to return a reference to one of the elements that it contains.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2018
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to call a self created method from an object stored in an ArrayList ?

    Quote Originally Posted by Norm View Post
    You need a reference to an instance of the class to be able to call its methods.
    To get at the contents of an ArrayList, call its get method to return a reference to one of the elements that it contains.
    Not sure I quite understand, as I made the change in the code below and am now getting error:

    Cpractice.java:14: error: 'void' type not allowed here
    System.out.println(bubba.get(i).printIndividualQue stionItem());

    1 error

    import java.util.*;
     
    public class Cpractice
    {
        public static void main(String[] args)
        {
            ArrayList<IndividualQuestionItem> bubba = new ArrayList<IndividualQuestionItem>();
     
            bubba.add( new IndividualQuestionItem("a", "b", "c", "d", "e") );
            bubba.add( new IndividualQuestionItem("2", "2", "2", "2", "2") );
     
            for (int i=0; i<bubba.size(); i++) 
                System.out.println(bubba.get(i).printIndividualQuestionItem()); 
     
            System.out.println("Finished");
        }
    }
     
     
    class IndividualQuestionItem
    {
        private String Question = "";
        private String CorrectAnswer = "";
        private String WrongAnswerA = "";
        private String WrongAnswerB = "";
        private String WrongAnswerC = "";
     
        // Constructor for the IndividualQuestionItem class.
        // It takes 5 temporary variables to initialize the question items.
        public IndividualQuestionItem(String a, String b, String c, String d, String e)
        {
            Question = a;
            CorrectAnswer = b;
            WrongAnswerA = c;
            WrongAnswerB = d;
            WrongAnswerC = e;
        }
     
        // Print out the  for the IndividualQuestionItem class.
        public void printIndividualQuestionItem()
        {
            System.out.print(this.Question + " ");
            System.out.print(this.CorrectAnswer + " ");
            System.out.print(this.WrongAnswerA + " ");
            System.out.print(this.WrongAnswerB + " ");
            System.out.println(this.WrongAnswerC);
            System.out.println();
        }
    }


    --- Update ---

    Thank you very much.
    I figured it out with your help.
    My mistake was that I was trying to call a void method from within a System.out.println().
    Once I got rid of that, all is well.
    Below is the code that works.

     
    import java.util.*;
     
    public class Cpractice
    {
        public static void main(String[] args)
        {
            ArrayList<IndividualQuestionItem> bubba = new ArrayList<IndividualQuestionItem>();
     
            bubba.add( new IndividualQuestionItem("a", "b", "c", "d", "e") );
            bubba.add( new IndividualQuestionItem("2", "2", "2", "2", "2") );
     
            for (int i=0; i<bubba.size(); i++) 
                bubba.get(i).printIndividualQuestionItem((IndividualQuestionItem) bubba.get(i)); 
     
            System.out.println("Finished");
        }
    }
     
     
    class IndividualQuestionItem
    {
        private String Question = "";
        private String CorrectAnswer = "";
        private String WrongAnswerA = "";
        private String WrongAnswerB = "";
        private String WrongAnswerC = "";
     
     
        // Constructor for the IndividualQuestionItem class.
        // It takes 5 temporary variables to initialize the question items.
        public IndividualQuestionItem(String a, String b, String c, String d, String e)
        {
            Question = a;
            CorrectAnswer = b;
            WrongAnswerA = c;
            WrongAnswerB = d;
            WrongAnswerC = e;
        }
     
     
        // Print out the  for the IndividualQuestionItem class.
        public void printIndividualQuestionItem(IndividualQuestionItem temp)
        {
            System.out.print(temp.Question + " ");
            System.out.print(temp.CorrectAnswer + " ");
            System.out.print(temp.WrongAnswerA + " ");
            System.out.print(temp.WrongAnswerB + " ");
            System.out.println(temp.WrongAnswerC);
            System.out.println();
        }
    }

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

    Default Re: How to call a self created method from an object stored in an ArrayList ?

    ok. I'm glad you got it working.

    The use of the printIndividualQuestionItem method is strange. Why pass a reference to another instance of the IndividualQuestionItem class to it. Why not have it print the contents of the instance of the class it is in?
    That could be done by removing temp as an argument to the method and removing the references to it.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Sep 2018
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to call a self created method from an object stored in an ArrayList ?

    You are correct.
    I removed the method argument, and it works just fine.
    Thank you.

Similar Threads

  1. how to call parameterized stored procedue in jdbc
    By olabanjitajudeen in forum JDBC & Databases
    Replies: 3
    Last Post: May 19th, 2014, 07:43 PM
  2. how to call parameterized stored procedue in jdbc
    By olabanjitajudeen in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 17th, 2014, 05:40 PM
  3. Replies: 1
    Last Post: May 18th, 2013, 11:28 PM
  4. Call a method in all instances of an object
    By MaximusPrime in forum Java Theory & Questions
    Replies: 2
    Last Post: March 14th, 2012, 10:39 PM
  5. [SOLVED] How to get the return of a method inside an object in an ArrayList?
    By Hallowed in forum Java Theory & Questions
    Replies: 7
    Last Post: May 1st, 2011, 10:44 PM