Results 1 to 16 of 16
  1. #1
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Converting Double to Int

    I am running this code (well it's a snippet at least)...
    Java Code:
    int number1;
    int number2;
    int Total
    
    number1 = 16 * 4
    number2 = 20 * 3
    
    Total = number1 + number2
    When doing a calculation does java auto convert Total to a Double even tho I have it declared as int?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Converting Double to Int

    Code Conventions for the Java Programming Language: Contents

    Variable names should start with a lowercase letter.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Converting Double to Int

    Quote Originally Posted by DarrylBurke View Post
    Code Conventions for the Java Programming Language: Contents

    Variable names should start with a lowercase letter.

    db
    Your right on that. I didn't mean to capitalize the "T" in total.

  5. #5
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Converting Double to Int

    Quote Originally Posted by Fubarable View Post
    Why do you think that it may convert the number?
    This is me totally guessing since I am a very newbie at Java, but when I run this code:
    Java Code:
    JOptionPane.showMessageDialog("The result of " + total );
    It gives me about 4 different errors, and one of them said something (I will give an exact error in a few) about wrong type Double should be Int or something of that nature. Which left me curious if Java would auto-convert Int to Double

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,797
    Rep Power
    19

    Default Re: Converting Double to Int

    Your best bet would be to show us the full errors and the code they are occurring on.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Converting Double to Int

    Here is the code I am using:
    Java Code:
    import javax.swing.JOptionPane;
    import java.lang.Math;
    public class TestThis
    {
    	public static void main (String args[])
    	{
    		String userInput;
    		int oneNumber;
    		double length;
    		double distance;
    		double allAround;
    		
    		userInput = JOptionPane.showInputDialog("Enter First Number");
    		
    		oneNumber = Integer.parseInt(userInput);
    		
    		length = (2 * oneNumber);
    		
    		distance = ((2 * Math.PI) * oneNumber);
    		
    		allAround = (Math.PI * oneNumber);
    		
    		JOptionPane.showMessageDialog(Null, + "The distance is " + oneNumber );
    		System.exit(0);
    	}
    }
    What is the best way to show the errors?

  8. #8
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,797
    Rep Power
    19

    Default Re: Converting Double to Int

    Copy and paste them into here.
    Presumably these are compilation errors?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,797
    Rep Power
    19

    Default Re: Converting Double to Int

    This line:
    Java Code:
          JOptionPane.showMessageDialog(Null, +"The distance is " + oneNumber);
    is completely wrong, though.
    What is 'Null'?
    And that first '+' is not needed.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Converting Double to Int

    Quote Originally Posted by Tolls View Post
    Copy and paste them into here.
    Presumably these are compilation errors?
    Nice I never knew you could copy/paste from the command prompt!! Here are the errors:

    c:\A_Programs>javac j.java
    j.java:19: error: possible loss of precision
    circumference = (2 * Math.PI * radius);
    ^
    required: int
    found: double
    j.java:21: error: possible loss of precision
    area = (Math.PI * radius);
    ^
    required: int
    found: double
    j.java:23: error: cannot find symbol
    JOptionPane.showMessageDialog(Null, + "The circumference is " +
    radius );
    ^
    symbol: variable Null
    location: class j
    j.java:23: error: bad operand type String for unary operator '+'
    JOptionPane.showMessageDialog(Null, + "The circumference is " +
    radius );
    ^
    4 errors

    c:\A_Programs>javac TestThis.java
    TestThis.java:23: error: cannot find symbol
    JOptionPane.showMessageDialog(Null, + "The distance is " + oneNumber );
    ^
    symbol: variable Null
    location: class TestThis
    TestThis.java:23: error: bad operand type String for unary operator '+'
    JOptionPane.showMessageDialog(Null, + "The distance is " + oneNumber );
    ^
    2 errors

  11. #11
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,797
    Rep Power
    19

    Default Re: Converting Double to Int

    Um, that's not the code you posted above.
    At least not the stuff about j.java.
    And the errors for TestThis.java are both on the line I highlighted...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Converting Double to Int

    Quote Originally Posted by Tolls View Post
    Um, that's not the code you posted above.
    At least not the stuff about j.java.
    And the errors for TestThis.java are both on the line I highlighted...
    Dangit your right, sorry I have like 3 different ones that I was trying to see if this way would work, okay what about this way. I'll check the line of code you posted above and see if I can tinker with it to get the code functioning properly.

  13. #13
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Converting Double to Int

    Quote Originally Posted by Tolls View Post
    This line:
    Java Code:
          JOptionPane.showMessageDialog(Null, +"The distance is " + oneNumber);
    is completely wrong, though.
    What is 'Null'?
    And that first '+' is not needed.
    Should the code just read like this:
    Java Code:
          JOptionPane.showMessageDialog("The distance is " + oneNumber);

  14. #14
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,797
    Rep Power
    19

    Default Re: Converting Double to Int

    Look at the API for JOptionPane and see, but as it stands that code is syntactically incorrect, before we even think about the API.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  15. #15
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Converting Double to Int

    I am copying this code:
    Java Code:
    JOptionPane.showMessageDialog(frame, "Eggs are not supposed to be green.");
    from this site:

    How to Make Dialogs (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

    And when I try to run it I get an error of:
    c:\A_Programs>javac tes.java
    tes.java:8: error: cannot find symbol
    JOptionPane.showMessageDialog(frame, "Eggs are not supposed to b
    e green.");
    ^
    symbol: variable frame
    location: class tes
    1 error

  16. #16
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,797
    Rep Power
    19

    Default Re: Converting Double to Int

    That's saying you don't have a 'frame' variable, so presumably you don't.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. converting double to pixel coordinates
    By mkj in forum New To Java
    Replies: 2
    Last Post: 02-05-2012, 06:43 PM
  2. Converting double to char array?
    By Blasz in forum New To Java
    Replies: 9
    Last Post: 08-29-2010, 07:32 PM
  3. Double.valueOf() vs Double.parseDouble()
    By greenbean in forum New To Java
    Replies: 10
    Last Post: 01-12-2009, 08:39 AM
  4. NullPointerException converting String to double
    By infaddict in forum New To Java
    Replies: 3
    Last Post: 07-19-2008, 06:01 PM
  5. Converting String to Double
    By srini in forum New To Java
    Replies: 1
    Last Post: 12-24-2007, 08:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •