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

Thread: Calling a print method from another class (printing array)

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Calling a print method from another class (printing array)

    Hi there,

    I've been stuck on this for a while and have tried everything I can think of. If I have two classes called FirstClass and SecondClass, and the FirstClass contains an array of objects that are objects from the SecondClass. Each object contains two ints and a string.

    I'm trying to create a method in the FirstClass that will go through each object in the array and print out the two ints and string held within that object.

    Am I correct when thinking I must create a printMethod in the SecondClass, then call on that printMethod from the FirstClass using the array? How do I go about doing this?

    I appreciate any help, and thanks


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Calling a print method from another class (printing array)

    If the printMethod in the FirstClass does everything that needs to be done to print the array, you can call that method from the SecondClass to do the printing.
    If you need other things done in the SecondClass, you could have a method there, but I don't see shy from what you have said.

    The array is in the FirstClass and the objects it contains were created in the SecondClass?

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Calling a print method from another class (printing array)

    I got it working I did have to call it from the second class, it was just some simple coding errors. But now I have a new problem. I'm trying to write a method to search my array list for the highest value in it. But objects in the array list have two ints and a string associated with them, so I need to some how make it search for one specific int. For example, search the number array to find which one has the highest 'int2' value.

        public void highestValue()
        {
            int i;
            int index = 0;
            int highest = numbers[0];
            for(i=0; i < numbers.length; i++)
            if(highest < numbers[i]) {
                highest = numbers[i];
                index = i;
            }
            return index;
        }

    I know I'm missing the parameters but I'm unable to get it to work with everything I try, also I'm not sure how to make it search that specific integer for each object in the array?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Calling a print method from another class (printing array)

    search the number array to find which one has the highest 'int2'
    If you have an array of objects and the objects contain more than one value.
    Add a method to the object to return the value that you want to compare against.
    Then have your search routine ask each object in the array as you look at it for the value to be compared using that method.

  5. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Calling a print method from another class (printing array)

    Quote Originally Posted by Norm View Post
    If you have an array of objects and the objects contain more than one value.
    Add a method to the object to return the value that you want to compare against.
    Then have your search routine ask each object in the array as you look at it for the value to be compared using that method.
    Thanks, but I'm not sure how to create the method that searches the objects in the array for the highest value. I know it will have to start at array object 0, then check if array object 1 is bigger than that, then check if 2 is bigger than 1, etc until the end of the array (similar to what I have above).

    I'm stuck but I'll keep trying and reading

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Calling a print method from another class (printing array)

    how to create the method that searches the objects in the array for the highest value
    You would use a loop like you posted in post #3

  7. #7
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Calling a print method from another class (printing array)

    Quote Originally Posted by Norm View Post
    You would use a loop like you posted in post #3
    This is what I have just now, the array list is called numbers. The problem is that when I run the method it asks me to enter a value? Am I doing something stupid/not understanding something obvious?

        public void highestValue(int[] numbers)
        {
            int i;
            int index = 0;
            int highest = numbers[0];
            for(i=0; i < numbers.length; i++)
            if(highest < numbers[i]) {
                highest = numbers[i];
                index = i;
            }
        }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Calling a print method from another class (printing array)

    when I run the method it asks me to enter a value
    I don't see anything in what you posted that asks the user for a value????
    numbers is an int array not an ArrayList.
    If you change the array to be an array of objects, you would replace the array reference operator: [i]
    with a call to a method in the object that would return the value that you are sorting on.
    The logic would remain the same, just the source of the values you are comparing would change.

Similar Threads

  1. Calling method from .class file
    By alexx_88 in forum Java Theory & Questions
    Replies: 6
    Last Post: April 24th, 2012, 02:14 AM
  2. Replies: 1
    Last Post: September 28th, 2011, 07:29 AM
  3. [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
  4. Help Calling Method From Another Class
    By CheekySpoon in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 15th, 2010, 10:24 AM
  5. Calling a void method into a static void main within same class
    By sketch_flygirl in forum Object Oriented Programming
    Replies: 3
    Last Post: November 15th, 2009, 05:24 PM