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: Calculate the average

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Calculate the average

    I wrote a code to calculate the average of numbers. The problem is that you only can insert whole numbers. Does anybody know how I can change the code so that I can insert float numbers?

    Here's the code i did with the help of a tutorial:

    Dies ist der Code:

    public class main_class {
    public static void main(String[] args){
    String sentinel = "";
    int sum = 0;
    int counter = 0;
    double mean = 0.0;
    Scanner NumScanner = new Scanner(System.in);

    System.out.println("Enter numbers to add. Enter \"d\" when done.");

    System.out.print("Enter number: ");
    sentinel = NumScanner.next();
    System.out.println();

    while(!sentinel.equals("d") && !sentinel.equals("D")) {
    sum += Integer.parseInt(sentinel);
    counter++;

    System.out.print("Enter number: ");
    sentinel = NumScanner.next();
    System.out.println();
    }

    mean = (sum*1.0)/counter;

    System.out.println();
    System.out.println("The arithmetic mean is: " + mean +".");
    }
    }


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

    Default Re: Calculate the average

    The Scanner class has methods for reading different types of numbers.
    Also the Double class has a method for converting a String to a double.
    Take a look at the API doc: Java Platform SE 7

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Calculate the average

    Original code below. Dont know the answer as im not too good at coding.

    public class main_class {
    public static void main(String[] args){
    String sentinel = "";
    int sum = 0;
    int counter = 0;
    double mean = 0.0;
    Scanner NumScanner = new Scanner(System.in);
     
    System.out.println("Enter numbers to add. Enter \"d\" when done.");
     
    System.out.print("Enter number: ");
    sentinel = NumScanner.next();
    System.out.println();
     
    while(!sentinel.equals("d") && !sentinel.equals("D")) {
    sum += Integer.parseInt(sentinel);
    counter++;
     
    System.out.print("Enter number: ");
    sentinel = NumScanner.next();
    System.out.println();
    }
     
    mean = (sum*1.0)/counter;
     
    System.out.println();
    System.out.println("The arithmetic mean is: " + mean +".");
    }
    }

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

    Default Re: Calculate the average

    I suggested two different ways to get float numbers:
    1) use a Scanner class method to read the data as float from the user
    2) use a Double or Float class method to convert the String read from the user to a double/float


    BTW The posted code has lost all its formatting. The statements should not all start in the first column. Nested statements should be indented.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Calculate the average

    Change the integers & doubles to floats and let the scanner read it as a float [scan.nextFloat();].
    Note: make sure you import the scanner class (java.util.Scanner.

  6. #6
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Calculate the average

    Thank You!

Similar Threads

  1. 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
  2. Help with Average and Variables Please =)
    By Raymond Pittman in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 9th, 2011, 10:02 AM
  3. Moving Average
    By Donieob in forum What's Wrong With My Code?
    Replies: 0
    Last Post: September 3rd, 2011, 08:58 AM
  4. Finding the Average
    By KiwiFlan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 16th, 2010, 07:58 PM
  5. Moving average
    By bondage in forum Loops & Control Statements
    Replies: 0
    Last Post: May 7th, 2010, 04:20 AM