Welcome to Dream.In.Code
Become a Java Expert!

Join 150,101 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,848 people online right now. Registration is fast and FREE... Join Now!




Calculations using user input numbers

 
Reply to this topicStart new topic

Calculations using user input numbers, Calculations using different data types

jlgraham
25 Jun, 2008 - 08:18 PM
Post #1

New D.I.C Head
*

Joined: 24 Jul, 2007
Posts: 5


My Contributions
I have an assignment that wants me to write an application that takes 5 numbers from a user. As the user inputs the numbers I have to make them into a double, float, long, int and byte. All I need after this is to output the result of the following calculation.
Subtract the floatValue from the intValue, then multiply by the doubleValue, then divide by the longValue, then multiply by the byteValue and then divide by 2. I am to store the result in an int variable. I can get the user input but am at a loss as to how to do the calculation and store the result. Can someone out in cyberworld help. Thanks to anyone who responds.
CODE
import java.util.*;


public class TestNumbers
{
    public static void main(String args[])
    {

        Scanner input = new Scanner(System.in);

        int result;

        System.out.println("Enter a number:");
        double num1 = input.nextDouble();


        System.out.println();
        System.out.println("Enter second number:");
        float num2 = input.nextFloat();


        System.out.println();
        System.out.println("Enter third number:");
        long num3 = input.nextLong();


        System.out.println();
        System.out.println("Enter fourth number:");
        int num4 = input.nextInt();


        System.out.println();
        System.out.println("Enter fifth number:");
        byte num5 = input.nextByte();


        System.out.println();

    }

}

User is offlineProfile CardPM
+Quote Post

pbl
RE: Calculations Using User Input Numbers
25 Jun, 2008 - 08:35 PM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
java

import java.util.*;


public class TestNumbers
{
public static void main(String args[])
{

Scanner input = new Scanner(System.in);


System.out.println("Enter a number:");
double num1 = input.nextDouble();


System.out.println();
System.out.println("Enter second number:");
float num2 = input.nextFloat();


System.out.println();
System.out.println("Enter third number:");
long num3 = input.nextLong();


System.out.println();
System.out.println("Enter fourth number:");
int num4 = input.nextInt();


System.out.println();
System.out.println("Enter fifth number:");
byte num5 = input.nextByte();

// Subtract the floatValue from the intValue,
double result = num3 - num2;
// then multiply by the doubleValue
result *= num1;
// then divide by the longValue
result /= num3;
//then multiply by the byteValue
result *= num5;
// then divide by 2.
result /= 2.0;
System.out.println("Result is: " + result);

}

}

User is offlineProfile CardPM
+Quote Post

jlgraham
RE: Calculations Using User Input Numbers
26 Jun, 2008 - 06:32 AM
Post #3

New D.I.C Head
*

Joined: 24 Jul, 2007
Posts: 5


My Contributions
Thanks for your help and I understand the reasoning but it still won't work. I have to store the result in an int variable and I get the error result already define in main double result = num3-num2;
I don't understand how to get the results to an int unless I can explicitly make it but how. More help please.
User is offlineProfile CardPM
+Quote Post

rgfirefly24
RE: Calculations Using User Input Numbers
26 Jun, 2008 - 08:09 AM
Post #4

D.I.C Regular
Group Icon

Joined: 7 Apr, 2008
Posts: 335



Thanked: 5 times
Dream Kudos: 150
My Contributions
QUOTE(jlgraham @ 26 Jun, 2008 - 07:32 AM) *

Thanks for your help and I understand the reasoning but it still won't work. I have to store the result in an int variable and I get the error result already define in main double result = num3-num2;
I don't understand how to get the results to an int unless I can explicitly make it but how. More help please.



ok first off, dont make a new post. Second try casting it to an int

java

int finalresult = Integer.parseInt(result);

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 12:33AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month