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

Thread: simple arithmetic problem

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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. #2
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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. #3
    Member
    Join Date
    Mar 2011
    Posts
    66
    My Mood
    Relaxed
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default Re: simple arithmetic problem

    Quote Originally Posted by nadrii View Post
    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. #4
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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);
    System.out.println("Please enter 2 integers:");
    num1 = scan.nextInt();
    num2 = scan.nextInt();

  5. #5
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

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

  6. #6
    Junior Member
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.
    Last edited by nadrii; October 26th, 2011 at 07:14 PM.

  7. #7
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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. #8
    Junior Member
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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. #9
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: simple arithmetic problem

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

  10. #10
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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:
    http://www.javaprogrammingforums.com...n-feeding.html

  11. #11
    Junior Member
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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. #12
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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. #13
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: simple arithmetic problem

    Try this mate

     
            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);
    Last edited by djl1990; October 26th, 2011 at 07:27 PM.

  14. #14
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: simple arithmetic problem

    Quote Originally Posted by copeg View Post
    @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.

  15. #15
    Junior Member
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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. #16
    Junior Member
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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!
    Last edited by nadrii; October 26th, 2011 at 10:34 PM.

Similar Threads

  1. Help with modifying a program for evaluating arithmetic expressions
    By rockout341 in forum Object Oriented Programming
    Replies: 5
    Last Post: October 13th, 2011, 11:01 AM
  2. Very simple problem...PLEASE HELP!
    By dungeondragon in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 1st, 2011, 07:19 AM
  3. Simple problem...
    By _lithium_ in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 6th, 2011, 12:02 AM
  4. doing arithmetic on Integer objects
    By gib65 in forum Java Theory & Questions
    Replies: 4
    Last Post: October 30th, 2010, 09:22 PM
  5. Arithmetic’s Game ??
    By holy crab in forum Algorithms & Recursion
    Replies: 0
    Last Post: April 12th, 2010, 04:27 AM