Results 1 to 5 of 5
  1. #1
    himanwish is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default convert integer to double

    Hi guys,

    I'm trying to get the average of two integer values in double.

    If I enter 7 and 8 then I get only 7 as the average not 7.5.

    I tried
    double average = Double.parseDouble( (firstNumber + secondNumber) /2 );
    but I get a compile error.

    below code is compiled fine but I cant get the average in double. Appreciate any ideas.

    import java.util.Scanner;

    public class Average {

    public static void main(String [] args) {

    Scanner console = new Scanner(System.in);

    System.out.print("Enter the first number");
    int firstNumber = console.nextInt();

    System.out.print("Enter the second number");
    int secondNumber = console.nextInt();


    double average = (firstNumber + secondNumber)/2;

    System.out.println("The average is " + average);
    }

    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Use a cast or divide by a double.

  3. #3
    himanwish is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    Thank you very much sunde887. it works!!!

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default

    Quote Originally Posted by sunde887 View Post
    Use a cast
    Just to clarify, you need to cast one or both of your ints to a double before the division. If you simply cast the result you will get 7.0 and not 7.5.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Quote Originally Posted by Junky View Post
    Just to clarify, you need to cast one or both of your ints to a double before the division. If you simply cast the result you will get 7.0 and not 7.5.
    Thanks for clarifying, I tend to be overly vague at times when posting from my phone.

    @op: you are welcome, please mark your thread solved with the thread tools if you are done.

Similar Threads

  1. Adding an integer to a double?
    By tokoolio in forum New To Java
    Replies: 5
    Last Post: 01-17-2011, 06:57 PM
  2. Replies: 2
    Last Post: 12-14-2010, 08:01 PM
  3. convert unsigned integer to signed integer in java?
    By diskhub in forum New To Java
    Replies: 6
    Last Post: 05-17-2010, 12:50 AM
  4. How to convert Integer[] to int[]
    By Nithya in forum New To Java
    Replies: 26
    Last Post: 02-11-2010, 05:41 PM
  5. How to extract integer value out of a double value?
    By javaexplorer in forum AWT / Swing
    Replies: 4
    Last Post: 01-16-2010, 05:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •