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

Thread: Int and Double...Performing Floating-Point Arithmetic help

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Location
    Virginia
    Posts
    4
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Int and Double...Performing Floating-Point Arithmetic help


    Hello Everyone,

    I am new Java Programming and I am struggling to pass my Java class. I have no idea on how to perform Java but I am trying lol. I am following assignments out of my work book and I have no idea as to what I am doing wrong. For this particular assignment I supposed to:

    * Change all variables' data types to double.
    * Change the two prompts to request double values
    * Change change the two calls to the nextInt() method to nextDouble().



    This is the original assignment:



    import java.util.Scanner;

    public class ArithmeticDemo
    {
    public static void main(String[] args)
    {
    Scanner input= new Scanner(System.in);

    int firstNumber;
    int secondNumber;
    int sum;
    int difference;
    int average;

    System.out.print("Please enter an integer>> ");
    firstNumber = input.nextInt();
    System.out.print ("Please enter another integer>> ");
    secondNumber = input.nextInt ();

    sum = firstNumber + secondNumber;
    difference = firstNumber - secondNumber;
    average = sum / 2;

    System.out.println(firstNumber + " + " + secondNumber + "is" +sum);
    System.out.println(firstNumber + " - " + secondNumber + "is" +difference);
    System.out.println("The average of " + firstNumber + " and " +secondNumber + "is" +average);


    }
    }



    This is the changes that I have made which are probably wrong:




    import java.util.Scanner;

    public class ArithmeticDemo2
    {
    public static void main(String[] args)
    {
    Scanner input= new Scanner(System.in);

    int firstNumber;
    int secondNumber;
    int sum;
    int difference;
    int average;

    System.out.print("Please enter an double>> ");
    firstNumber = input.nextDouble();
    System.out.print ("Please enter another double>> ");
    secondNumber = input.nextDouble();

    sum = firstNumber + secondNumber;
    difference = firstNumber - secondNumber;
    average = sum / 2;

    System.out.println(firstNumber + " + " + secondNumber + "is" +sum);
    System.out.println(firstNumber + " - " + secondNumber + "is" +difference);
    System.out.println("The average of " + firstNumber + " and " +secondNumber + "is" +average);

    }
    }


    This is the error that I am getting:



    G:\ArithmeticDemo2.java:16: error: possible loss of precision
    firstNumber = input.nextDouble();
    ^
    required: int
    found: double
    G:\ArithmeticDemo2.java:18: error: possible loss of precision
    secondNumber = input.nextDouble();
    ^
    required: int
    found: double
    2 errors

    Tool completed with exit code 1


    Can someone please tell me what I did wrong or what I am missing? The work book that is assigned for my class is not helpful at all

    Thanks so much!!


  2. #2
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: Int and Double...Performing Floating-Point Arithmetic help

    You need to change the data types of those to doubles. Right now they are declared as ints

  3. The Following User Says Thank You to camel-man For This Useful Post:

    kkco83 (September 19th, 2014)

  4. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Int and Double...Performing Floating-Point Arithmetic help

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly using code or highlight tags per the above link.

  5. #4
    Junior Member
    Join Date
    Sep 2014
    Location
    Virginia
    Posts
    4
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Int and Double...Performing Floating-Point Arithmetic help

    Quote Originally Posted by GregBrannon View Post
    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly using code or highlight tags per the above link.
    Sorry..I tried to post my code in red but it wouldn't work. Not sure if it was because I copy and pasted the code.

Similar Threads

  1. Performing operations without using ARithmetic Operators
    By farrahdee in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 24th, 2013, 07:49 AM
  2. Replies: 4
    Last Post: August 27th, 2013, 11:28 PM
  3. cannot enter a floating point number
    By Tedstriker in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 5th, 2013, 06:37 PM
  4. regarding floating point
    By deependeroracle in forum Java Theory & Questions
    Replies: 3
    Last Post: January 10th, 2012, 11:18 AM
  5. [SOLVED] Performing Division with Double Variables
    By bgroenks96 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 5th, 2011, 05:36 PM