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: How do I get an average using an Array?

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How do I get an average using an Array?

    I'm trying to calculate the average of grades that a user puts in.

    Here is my code so far:
     public static void main(String[] args) {
     
            Scanner input = new Scanner (System.in);
     
            System.out.println("Please enter an array of grades seperated bt a comma.");
            input.nextLine();
            String arrayOfGrades = "100,50,100";
            String[] grades = arrayOfGrades.split(",");
     
     
     
        }
     
        public static double average(String[] grades){
     
            double sum=0;
            double average;
     
            for (int i=0; i<grades.length;i++){
                sum += grades[i];
            }
            average = sum/grades.length;
     
            System.out.println("Your grade average is:" +average);
        }

    I think I'm on the right track, the only big error I'm really getting is the line: sum += grades[i]. It's saying string can not be converted into a double.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How do I get an average using an Array?

    string can not be converted into a double.
    Basically that's true about what the compiler can NOT do. The programmer can write code that uses the Double class's parse method to convert a String of digits (like "12.34") to a double.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: How do I get an average using an Array?

    Why are you using a string? You should always assume a double when dealing with grades. Just make a double array or even better an ArrayList.

    parseDouble
    returns a primitive double containing the value of the string:

    "Returns a new double initialized to the value represented by the specified String, as performed by the valueOf method of class Double."

    valueOf
    returns a Double instance, if already cached, you'll get the same cached instance.

    "Returns a Double instance representing the specified double value. If a new Double instance is not required, this method should generally be used in preference to the constructor Double(double), as this method is likely to yield significantly better space and time performance by caching frequently requested values."

    To avoid the overhead of creating a new Double object instance, you should normally use valueOf
    Last edited by jocdrew21; April 22nd, 2014 at 05:58 AM.

Similar Threads

  1. [SOLVED] Average Rainfall Main Class Using nested for loops,input validation - Average is off
    By CyberOps in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 24th, 2014, 05:36 AM
  2. use array of scores to calculate an average
    By littlebit45 in forum Other Programming Languages
    Replies: 1
    Last Post: November 30th, 2012, 03:25 PM
  3. Print Student average, high, and low grades through array?
    By MLeclerc182 in forum Object Oriented Programming
    Replies: 2
    Last Post: May 10th, 2012, 06:12 AM
  4. Calculating average of an array
    By Vika in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 29th, 2011, 08:06 AM
  5. Average program with array and loop and JOptionPane.
    By jeremykatz in forum AWT / Java Swing
    Replies: 6
    Last Post: October 25th, 2009, 02:33 PM