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

Thread: Returning value from method? Arrays-

  1. #1
    Member
    Join Date
    Oct 2013
    Posts
    31
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Returning value from method? Arrays-

    Hello everyone,

    Here is the problem that I am trying to solve: Write a method that is passed two arrays. Your method should return true if the two arrays have the same size and the same contents. Otherwise, return false.


    I am receiving the following error in BlueJ - ".class expected" on this line:

    System.out.println(f(int[] a, int[] b));    // print out the value returned by f

    Any help would be appreciated very much.



    Here is what I have completed so far:


     
    class Num2
    {
        public static void main (String[] args)
        {
            int[] a = {3, 4, 5, 6, 7};
            int[] b = {5, 8, 9, 10, 5};
            f(a, b);                  // call the f method
            System.out.println(f(int[] a, int[] b));    // print out the value returned by f
        }
     
        public static boolean f(int[] a, int[] b)
        {
            if (a.length == b.length)
            {
                for (int i = 0; i < a.length; i++)
                {
                    if (a[i] != b[i])
                    return false;
     
                }
                return true;
                   }
     
            else
            return false;
     
             } 
     
    }


  2. #2
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: Returning value from method? Arrays-

    You see how you are calling the method above the printin, put it in the println that way and get rid of what you have.

    It thinks you're referring to a class or something. Java has array params in method calls the same way everything else is put in a method call (i.e. with only the variable name, the array param syntax is only need in the params to the actual method body, not the method call)).

  3. The Following User Says Thank You to GoodbyeWorld For This Useful Post:

    TSSF44 (October 27th, 2013)

  4. #3
    Member
    Join Date
    Oct 2013
    Posts
    31
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Returning value from method? Arrays-

    Quote Originally Posted by GoodbyeWorld View Post
    You see how you are calling the method above the printin, put it in the println that way and get rid of what you have.

    It thinks you're referring to a class or something. Java has array params in method calls the same way everything else is put in a method call (i.e. with only the variable name, the array param syntax is only need in the params to the actual method body, not the method call)).
    Thank you! That makes sense. I had no idea what I was doing wrong.

Similar Threads

  1. help with creating a method and returning a value
    By kungfuthug in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 17th, 2013, 07:22 PM
  2. Help with returning variable from method
    By m7abraham in forum What's Wrong With My Code?
    Replies: 19
    Last Post: September 17th, 2012, 03:01 PM
  3. Method returning 0 for everything
    By JJTierney in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 4th, 2010, 08:51 PM
  4. Method returning boolean
    By Plural in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 13th, 2010, 06:45 PM
  5. Arrays.equals not returning correct answer.
    By Anyone in forum Collections and Generics
    Replies: 2
    Last Post: February 11th, 2010, 10:12 AM