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

1. ## 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?

2. ## 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?

3. ## Re: simple arithmetic problem

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

4. ## 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:

```int num1, num2;
Scanner scan = new Scanner (System.in);
num1 = scan.nextInt();
num2 = scan.nextInt();```

5. ## Re: simple arithmetic problem

Ah i think its parseDouble you need to convert int to double.

at the moment i have:

```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);
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

6. ## 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:

```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.

7. ## 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.

8. ## Re: simple arithmetic problem

Well right now I'm trying the following:

```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.

9. ## Re: simple arithmetic problem

The key point learnt here i think is converting the ints to doubles before performing the division.

10. ## 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
`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:
The Problem with Spoon-feeding

11. ## Re: simple arithmetic problem

```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

12. ## 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.

13. ## Re: simple arithmetic problem

Try this mate

```
int A, B;
double C, D, E;
Scanner scan = new Scanner(System.in);
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);```

14. ## Re: simple arithmetic problem

Originally Posted by copeg
@djl1990, I recommend you read the following link - a few of your posts come quite close:
The Problem with Spoon-feeding

Sorry, I do try and explain steps though. I wont post code solutions in future.

15. ## Re: simple arithmetic problem

Ooo, that worked! I realized just now I had set E to a float a while back as I was trying to solve this problem. Converting them to doubles using this method worked though.

My only issue now, though, is limiting E to two decimal places. Would you happen to know how I can do that?

16. ## Re: simple arithmetic problem

I'm looking around a bit and seeing some stuff about using pound signs to designate how many decimal points will be printed out. Anyone know anything about this?

edit:
This post cleared it up for me. Thanks a lot!