# simple arithmetic problem

• October 26th, 2011, 07:07 PM
simple arithmetic problem
Hello. I'm new here and simply looking for help to a problem I'm having.

I have a program requesting user input for two variables as integers. I then have a third variable set as a double that I would like to use to divide the two integers and give me a decimal with no more than 2 decimal places.

I'm fairly certain this has something to do with me trying to set a double equal to the division of 2 integers. How would I get this to work?
• October 26th, 2011, 07:46 PM
djl1990
Re: simple arithmetic problem
so you want to ask the user to type in 2 integers?

then divide the first integer by the second integer? and store this value in a double.

then print out the double to 2 decimal places?
• October 26th, 2011, 07:53 PM
Actinistia
Re: simple arithmetic problem
Quote:

I'm fairly certain this has something to do with me trying to set a double equal to the division of 2 integers. How would I get this to work?

Well first of all, if you divide an int by an int, you get a truncated (or cut off) answer, setting the answer as a double simply adds .0 at the end. What is it that you're confused with? Are you trying to add the two ints and divide by a double? If so, are you confused with how to have the answer go to 2 decimal places? How much have you already done and what specifically do you have trouble with?

*edit* darn, too slow once again :(
• October 26th, 2011, 07:56 PM
djl1990
Re: simple arithmetic problem
to read in user input you need to use the Scanner class.

first of all import the Scanner class: import java.util.Scanner;

then in your main class body you want to create a new scanner object and read in 2 integers from the user.

use this:

Code :

```int num1, num2; Scanner scan = new Scanner (System.in); System.out.println("Please enter 2 integers:"); num1 = scan.nextInt(); num2 = scan.nextInt();```
• October 26th, 2011, 08:04 PM
djl1990
Re: simple arithmetic problem
Ah i think its parseDouble you need to convert int to double.

at the moment i have:

Code :

```package testing;   import java.util.Scanner; import java.text.DecimalFormat;   public class NewMain {   public static void main(String[] args) {   int num1, num2; double num3, num4, divided; Scanner scan = new Scanner(System.in); System.out.println("Please enter 2 integers:"); num1 = scan.nextInt(); num2 = scan.nextInt(); num3 = Double.parseDouble(Integer.toString(num1)); num4 = Double.parseDouble(Integer.toString(num2)); divided = num3 / num4; System.out.println(divided); } }```

it reads in 2 integers, converts them to doubles, stores the division result in a double called divided.

Now you somehow need to format the double 'divided' to 2 decimal places
• October 26th, 2011, 08:07 PM
Re: simple arithmetic problem
The entire program works just fine. I've scanned in two separate integers using two separate print statements. They're now stored in two separate variables, let's say A and B (for reference).

I now have a third variable C declared as a double. I would like to do the following:

Code :

```C = A / B; System.out.print("The value of C is " + C + ". ");```

However, C always comes out as 0 even though A and B up to this point are being calculated just fine. In my code right now I have print statements verifying this. I'd like to print C so that it actually does this division properly. I figured the problem lied with dividing two integers and setting that equal to a double. I'd like to know how I can get this to work.

edit:
Just saw the above post, going to try it.
• October 26th, 2011, 08:12 PM
djl1990
Re: simple arithmetic problem
Yes you should try my code above to see if its what youre looking for.

I had the same problem with dividing two integers and trying to just store the result as a double.

it just gave 0.0 everytime.
• October 26th, 2011, 08:14 PM
Re: simple arithmetic problem
Well right now I'm trying the following:

Code :

```A = (int) Double.parseDouble(Integer.toString(A)); B = (int) Double.parseDouble(Integer.toString(B));   C = A / B; System.out.print("The value of C is " + C + ". ");```

The reason that int in parenthesis is added is because my compiler is suggesting it as a solution to an error. The way you presented it to me wasn't working. And after I put that int there C is still coming out as 0.0.
• October 26th, 2011, 08:15 PM
djl1990
Re: simple arithmetic problem
The key point learnt here i think is converting the ints to doubles before performing the division.
• October 26th, 2011, 08:19 PM
copeg
Re: simple arithmetic problem
If you want a double from the division of the int's, you need to specify you are doing floating point math by casting the num/denom to double types
Code :

`double d = a/(double)b;//assuming a and b are of type int`

@djl1990, I recommend you read the following link - a few of your posts come quite close:
http://www.javaprogrammingforums.com...n-feeding.html
• October 26th, 2011, 08:20 PM
Re: simple arithmetic problem
Code :

```int test1, test2;   test1 = (int) Double.parseDouble(Integer.toString(A)); test2 = (int) Double.parseDouble(Integer.toString(B));   C = test1 / test2; System.out.print("The value of C is " + C + ". ");```

Still getting 0.0 :(
• October 26th, 2011, 08:20 PM
djl1990
Re: simple arithmetic problem
You'll need 5 variables

A & B (the two integers read in from user input)
C = A parsed to a double
D = B parsed to a double
and E = C / B

Try it that way.
• October 26th, 2011, 08:23 PM
djl1990
Re: simple arithmetic problem
Try this mate

Code :

```  int A, B; double C, D, E; Scanner scan = new Scanner(System.in); System.out.println("Please enter 2 integers:"); A = scan.nextInt(); B = scan.nextInt(); C = Double.parseDouble(Integer.toString(A)); D = Double.parseDouble(Integer.toString(B)); E = C / D; System.out.println("1st number divided by the 2nd number = " + E);```
• October 26th, 2011, 08:25 PM
djl1990
Re: simple arithmetic problem
Quote:

Originally Posted by copeg
@djl1990, I recommend you read the following link - a few of your posts come quite close:
http://www.javaprogrammingforums.com...n-feeding.html

Sorry, I do try and explain steps though. I wont post code solutions in future.
• October 26th, 2011, 08:27 PM