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

Thread: Casting int and doubles variables

  1. #1
    Member
    Join Date
    Jul 2013
    Location
    Mars
    Posts
    50
    My Mood
    Relaxed
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Casting int and doubles variables

    Hello,

    I am having an issue with casting. See code below:

    class GDPPerCapita
    {
    	public static void main(String[] args)
    	{
    	// Declare variables
    	int gdp = 175000;
    	int population = 4000;
    	double gdpPerCapita = (double)gdp/population; // = 43.75
    	int gdpPerCapitaDollars = (int)gdpPerCapita; // = 43
    	int gdpPerCapitaCents = gdpPerCapita - gdpPerCapitaDollars; // Should equal 75 as an int, but isn't
     
    	System.out.println("GDP: $" + gdp);
    	System.out.println("Population: " + population);
    	System.out.println("GDP Per Capita: $" + gdpPerCapita);
    	System.out.println("GDP Per Capita: " + gdpPerCapitaCents + "Dollars and " + gdpPerCapitaCents + " Cents");
     
    	}
    }

    Desired output should look as follows:

    GDP: $175000
    Population: 4000
    GDP Per Capita: $43.75
    GDP Per Capita: 43 Dollars and 75 Cents

    I was able to cast gdpPerCapita and gdpPerCapitaDollars without an issue. The problem comes when trying to get the remainder (75). I have tried

    int gdpPerCapitaCents = (int)gdpPerCapita - gdpPerCapitaDollars;

    And

    int gdpPerCapitaCents = (double)gdpPerCapita - gdpPerCapitaDollars;

    And

    int gdpPerCapitaCents = (int)gdpPerCapitaDollars - gdpPerCapita;

    And lastly

    int gdpPerCapitaCents = (double)gdpPerCapitaDollars - gdpPerCapita;

    I'm sure I am just doing something slightly wrong here, as i have never tried to use arithmetic operators on casted variables. Uber confusion here, any help appreciated.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,465
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: Casting int and doubles variables

    You say you have a problem but do not explain what that problem is. Perhaps post the desired output and the actual output you are getting.
    Improving the world one idiot at a time!

  3. #3
    Member
    Join Date
    Jul 2013
    Location
    Mars
    Posts
    50
    My Mood
    Relaxed
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Casting int and doubles variables

    The problem comes when trying to get the remainder (75).
    again the issue is when trying to find the remainder of gdpPerCapita - gdpPerCapitaDollars in int form. It should come to 75, but when i try run the program with any of these for the value of gdpPerCapitaCents


    int gdpPerCapitaCents = (int)gdpPerCapita - gdpPerCapitaDollars;

    And

    int gdpPerCapitaCents = (double)gdpPerCapita - gdpPerCapitaDollars;

    And

    int gdpPerCapitaCents = (int)gdpPerCapitaDollars - gdpPerCapita;

    And lastly

    int gdpPerCapitaCents = (double)gdpPerCapitaDollars - gdpPerCapita;

    It will either not compile, or it will give me a value of 0, or it will give me this error:

    GDPPerCapita.java:10: error: possible loss of precision
    	int gdpPerCapitaCents = (double)gdpPerCapita - (double)gdpPerCapitaDollars;
    	                                             ^
      required: int
      found:    double
    1 error

    If I'm just explaining my issue incorrectly I'm sorry, trying the best i can i guess.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,465
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: Casting int and doubles variables

    When the double 43.75 is cast to an int you get 43. Obviously 43 - 43 = 0.
    What happens if you subtract gdpPerCapitaDollars from gdpPerCapita without casting? Note you will need to assign the result to a double.
    Improving the world one idiot at a time!

  5. #5
    Member
    Join Date
    Jul 2013
    Location
    Mars
    Posts
    50
    My Mood
    Relaxed
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Casting int and doubles variables

    Yes that makes perfect sense. When i try to keep gdpPerCapitaCents as type int (as I'm instructed in my assignment for school, unfortunately) and declare the value of it as gdpPerCapita - gdpPerCapitaDollars:

    class GDPPerCapita
    {
    	public static void main(String[] args)
    	{
    	// Declare variables
    	int gdp = 175000;
    	int population = 4000;
    	double gdpPerCapita = (double)gdp/population; // = 43.75
    	int gdpPerCapitaDollars = (int)gdpPerCapita; // = 43
    	int gdpPerCapitaCents = gdpPerCapita - gdpPerCapitaDollars;
     
    	System.out.println("GDP: $" + gdp);
    	System.out.println("Population: " + population);
    	System.out.println("GDP Per Capita: $" + gdpPerCapita);
    	System.out.println("GDP Per Capita: " + gdpPerCapitaDollars + " Dollars and " +
    	gdpPerCapitaCents + "Cents" );
     
    	}
    }

    This is the error that i get:

    GDPPerCapita.java:10: error: possible loss of precision
    	int gdpPerCapitaCents = gdpPerCapita - gdpPerCapitaDollars;
    	                                     ^
      required: int
      found:    double
    1 error

    Even when i declare gdpPerCapitaCents into type double (which is not how my i am instructed to complete this assignment), the output is:

    GDP: $175000
    Population: 4000
    GDP Per Capita: $43.75
    GDP Per Capita: 43 Dollars and 0.75Cents

    When in the final line, should read "43 Dollars and 75 Cents", that is why the variable gdpPerCapita needs to be an int so that i can ditch the decimal point. Again I'm trying to explain this as best i can with the limited knowledge that i have in all of this. If I'm missing something that will help you help me please let me know.

  6. #6
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default

    If you're getting 0.75 as a double value. What do you need to multiply that by to get 75.0? Then store as an int and you get...?

    You're nearly there.

  7. #7
    Member
    Join Date
    Jul 2013
    Location
    Mars
    Posts
    50
    My Mood
    Relaxed
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Casting int and doubles variables

    So here's the part of the assignment to initialize gdpPerCapitaCents:

    Declare an 'int' type variable called 'gdpPerCapitaCents' and assign it only the "cents" part of the value from 'gdpPerCapita'. So if the 'gdpPerCapita' = 43.75, then 'gdpPerCapitaCents' should contain 75. [Note: Do NOT "hard-code" its value as "75". Instead, compute that value from an arithmetic expression using 'gdpPerCapitaDollars' and 'gdpPerCapita'].
    This is where i am having trouble.

    --- Update ---

    The part i don't understand is how to have my gdpPerCapitaCents variable set as an int, then take the value of one double, and one int to get a final value of an int. I have been sitting trying to cast int or double into the expression with zero success, or try rearranging from (gdpPerCapita - gdpPerCapitaDollars) to (gdpPerCapitaDollars - gdpPerCapita) with a very familiar NO success. I've tried googling how to cast from an int to a double, and vice versa but i just feel as if i have hit a wall here, and it hurts haha.

  8. #8
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,465
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: Casting int and doubles variables

    Kewish just told you how. They took my advice and expanded upon it. Which I had hoped you would be able to figure it out for yourself.
    Improving the world one idiot at a time!

  9. #9
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default

    What is .75 multiplied by 100?
    How would you work that into your equation?

  10. #10
    Member
    Join Date
    Jul 2013
    Location
    Mars
    Posts
    50
    My Mood
    Relaxed
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Casting int and doubles variables

    Ok please tell me i am close here. I got it to compile and run. I used gdpPerCapitaCents as a double:

    class GDPPerCapita
    {
    	public static void main(String[] args)
    	{
    	// Declare variables
    	int gdp = 175000;
    	int population = 4000;
    	double gdpPerCapita = (double)gdp/population; // = 43.75
    	int gdpPerCapitaDollars = (int)gdpPerCapita; // = 43
    	double gdpPerCapitaCents = (gdpPerCapita - gdpPerCapitaDollars) * 100;
     
    	System.out.println("GDP: $" + gdp);
    	System.out.println("Population: " + population);
    	System.out.println("GDP Per Capita: $" + gdpPerCapita);
    	System.out.println("GDP Per Capita: " + gdpPerCapitaDollars + " Dollars and " +
    	gdpPerCapitaCents + " Cents" );
     
    	}
    }

    And that makes sense, and gives me the output like this:

    GDP: $175000
    Population: 4000
    GDP Per Capita: $43.75
    GDP Per Capita: 43 Dollars and 75.0 Cents

    It still has the decimal, but i know i could get rid of it by doing this:

    class GDPPerCapita
    {
    	public static void main(String[] args)
    	{
    	// Declare variables
    	int gdp = 175000;
    	int population = 4000;
    	double gdpPerCapita = (double)gdp/population; // = 43.75
    	int gdpPerCapitaDollars = (int)gdpPerCapita; // = 43
    	double gdpPerCapitaCents = (gdpPerCapita - gdpPerCapitaDollars) * 100;
    	int gdpCentsInt = (int)gdpPerCapitaCents; // Make the double variable into an integer
     
    	System.out.println("GDP: $" + gdp);
    	System.out.println("Population: " + population);
    	System.out.println("GDP Per Capita: $" + gdpPerCapita);
    	System.out.println("GDP Per Capita: " + gdpPerCapitaDollars + " Dollars and " +
    	gdpCentsInt + " Cents" );
     
    	}
    }

    To get this output:

    GDP: $175000
    Population: 4000
    GDP Per Capita: $43.75
    GDP Per Capita: 43 Dollars and 75 Cents

    But that is still not what i am "supposed to do". Everything i try to keep gdpPerCapitaCents set as an integer will not work for me. I promise i am not trying to be a pain here and i appreciate all of the replies. Been working on this tiny code for... 5 hours now, awesome!

  11. #11
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,465
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: Casting int and doubles variables

    How many fargin times do you have to be told?

    43.75 - 43 = 0.75
    0.75 * 100 = 75.0
    (int) 75.0 = 75
    Improving the world one idiot at a time!

  12. #12
    Member
    Join Date
    Jul 2013
    Location
    Mars
    Posts
    50
    My Mood
    Relaxed
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Casting int and doubles variables

    Right, so, this then right???

    int gdpPerCapitaCents = (gdpPerCapita - gdpPerCapitaDollars) * 100;


    That is saying (43.75 - 43) x 100.

    I get what you are saying, and i know spoon feeding is frowned upon here. But i am seriously not picking up on how to take my INTEGER gdpPerCapitaCents variable, and do these things so that at the END of the statement it equals 43, only because of casting, not because of the math behind it.

  13. #13
    Member
    Join Date
    Jul 2013
    Location
    Mars
    Posts
    50
    My Mood
    Relaxed
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Casting int and doubles variables

    Bump, can anyone show where i am being stupid? Just don't say everywhere

  14. #14
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default Re: Casting int and doubles variables

    Taken from your post. Just added casting and another set of parentheses.
    int gdpPerCapitaCents = (int)((gdpPerCapita - gdpPerCapitaDollars) * 100); // 75

    Remember it does the math in the parenthesis and then finally casts as an int. If you don't have the parentheses it will output 0. Casting is a right to left associativity.

  15. The Following User Says Thank You to Kewish For This Useful Post:

    ///M Dood (September 16th, 2013)

  16. #15
    Member
    Join Date
    Jul 2013
    Location
    Mars
    Posts
    50
    My Mood
    Relaxed
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Casting int and doubles variables

    YOU ARE AMAZING thank you so much! I tried variations of that for hours upon hours. You don't even understand. The one thing i never did try was putting the * 100 into parenthesis. Jeez, unfortunately the answer really was in this thread the whole time and i was too dumb to be able to make sense of it. Seriously thank you again, you just relieved my massive headache.

  17. #16
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,465
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: Casting int and doubles variables

    A lesson to be learnt: I gave you the algorithm in several steps. If you cannot work it out in one (hard to read) line of code then break it down into several steps.
    Improving the world one idiot at a time!

  18. #17
    Member
    Join Date
    Jul 2013
    Location
    Mars
    Posts
    50
    My Mood
    Relaxed
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Casting int and doubles variables

    I tried that (kind of). Where i got confused is for this (int)((gdpPerCapita - gdpPerCapitaDollars) * 100); i totallyspaced on the whole order of operation thing. The whole math expression inside of one set of parenthesis, with the minus part of it in separate parenthesis, with the whole expression casted into an integer type at the beginning, but outside of the math part. Believe me, this whole situation was nothing more than UBER confusion, not laziness. I was messing with that one line of code for so "farging" long last night and into today. And i really thought i was trying what you guys were suggesting.

Similar Threads

  1. How to create an Int or double of what the user types int a JTextField?
    By Speedstack79 in forum Object Oriented Programming
    Replies: 2
    Last Post: January 13th, 2013, 10:00 PM
  2. Replies: 2
    Last Post: October 5th, 2012, 01:14 PM
  3. List methods add(int k, Data data), set(int k, Data data), remove(int k)
    By Enirox in forum Object Oriented Programming
    Replies: 3
    Last Post: September 20th, 2012, 06:43 AM
  4. [SOLVED] why casting int to String is not possible through brackets method
    By voltaire in forum Java Theory & Questions
    Replies: 2
    Last Post: May 2nd, 2010, 04:00 PM
  5. reading string input then casting it to an int?
    By etidd in forum Java Theory & Questions
    Replies: 2
    Last Post: March 27th, 2010, 11:49 PM