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

Thread: Array issue

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Array issue

    Can someone help me fix this array. I have attached the link to my code and below is a question that goes with it. I have about 98% of it done so far but something seems to be missing. Not sure how to fix it.

    import java.util.ArrayList; public class ArrayDemo { public static - Pastebin.com

    1. The class should have a main method which will declare and initialize an array containing the following numbers:

    2.0, 3.5, 6.9, 8.25, 12.5

    2. The class should have a method called calculateAverage which will take an array as a parameter and return its average as a double. This method should demonstrate the correct way to obtain the number of elements in the array and not assume that there will always be 5 elements.

    3. The main method should call the calculateAverage method to get the average of the array created in step 1.


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Array issue

    You'll be much more likely to get some help if you post your code here (wrapped in code tags) along with any output or errors it gives you.

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,549
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: Array issue

    Quote Originally Posted by Mini83 View Post
    Not sure how to fix it.
    Neither do we if you don't tell us what is wrong.
    Improving the world one idiot at a time!

  4. #4
    Member
    Join Date
    Aug 2011
    Posts
    86
    My Mood
    Lurking
    Thanks
    16
    Thanked 4 Times in 4 Posts

    Default Re: Array issue

    I don't think you can put anything, i.e. your calcualteAvaerage() inside of your main. Move that out and put it before your main class.
    Also, java don't care, but Avaerage is misspelled. it should be Average.

    Alternatively you can make calcualteAverage() a separate calculateAverage.java, and start it with the "package calculateAverage;" line.
    Add the line "import calculateAverage.*;" to ArrayDemo.java

    make a new folder and call it ArrayDemo. inside this folder make another new folder and call it calculateAverage.
    put ArrayDemo.java in the ArrayDemo folder. put calculateAverage.java in the calculateAverage folder.

    open command prompt and navigate to ArrayDemo folder.
    at the prompt type "javac ArrayDemo.java calculateAverage/calculateAverage.java
    then type "java ArrayDemo" to see if it works.
    Last edited by Spidey1980; August 18th, 2011 at 12:23 AM.

  5. #5
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Array issue

    Quote Originally Posted by Spidey1980 View Post
    I don't think you can put anything, i.e. your calcualteAvaerage() inside of your main.
    That isn't true. OP has declared calcualteAverage() as static, so it's perfectly legal to call it inside main.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,335
    Thanks
    181
    Thanked 830 Times in 773 Posts
    Blog Entries
    5

    Default Re: Array issue

    Alternatively you can make calcualteAverage() a separate calculateAverage.java, and start it with the "package calculateAverage;" line.
    Add the line "import calculateAverage.*;" to ArrayDemo.java
    And why do this? Java is object oriented, and I fail to see how 'calculateAverage' represents an object, let alone a package...better advice would be to suggest a new class, perhaps call it MathUtils, which contains the calculateAverage method, and place this within a package more appropriately named (for example util)
    Last edited by copeg; August 18th, 2011 at 09:21 AM.

Similar Threads

  1. Jgrasp issue
    By Joy123 in forum Java Theory & Questions
    Replies: 1
    Last Post: June 28th, 2011, 10:39 PM
  2. [SOLVED] if statement issue
    By Elementality in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 17th, 2011, 02:29 PM
  3. [SOLVED] Calendar Issue
    By aussiemcgr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 11th, 2010, 01:19 PM
  4. i do not know how to solve this issue
    By javastupi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 20th, 2010, 07:28 PM
  5. url pattern issue - please help me...
    By bharathik in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: November 9th, 2009, 03:28 AM