Converting Double to Int

• 05-01-2012, 04:22 AM
Rahim2312
Converting Double to Int
I am running this code (well it's a snippet at least)...
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?
• 05-01-2012, 04:55 AM
Fubarable
Re: Converting Double to Int
Why do you think that it may convert the number?
• 05-01-2012, 05:37 AM
DarrylBurke
Re: Converting Double to Int
Code Conventions for the Java Programming Language: Contents

db
• 05-01-2012, 02:54 PM
Rahim2312
Re: Converting Double to Int
Quote:

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

db

Your right on that. I didn't mean to capitalize the "T" in total.
• 05-01-2012, 02:56 PM
Rahim2312
Re: Converting Double to Int
Quote:

Originally Posted by Fubarable
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:
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
• 05-01-2012, 03:27 PM
Tolls
Re: Converting Double to Int
Your best bet would be to show us the full errors and the code they are occurring on.
• 05-01-2012, 03:30 PM
Rahim2312
Re: Converting Double to Int
Here is the code I am using:
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?
• 05-01-2012, 04:07 PM
Tolls
Re: Converting Double to Int
Copy and paste them into here.
Presumably these are compilation errors?
• 05-01-2012, 04:08 PM
Tolls
Re: Converting Double to Int
This line:
Code:

`      JOptionPane.showMessageDialog(Null, +"The distance is " + oneNumber);`
is completely wrong, though.
What is 'Null'?
And that first '+' is not needed.
• 05-01-2012, 04:11 PM
Rahim2312
Re: Converting Double to Int
Quote:

Originally Posted by Tolls
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
^
required: int
found: double
j.java:23: error: cannot find symbol
JOptionPane.showMessageDialog(Null, + "The circumference is " +
^
symbol: variable Null
location: class j
j.java:23: error: bad operand type String for unary operator '+'
JOptionPane.showMessageDialog(Null, + "The circumference is " +
^
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
• 05-01-2012, 04:59 PM
Tolls
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...
• 05-01-2012, 05:05 PM
Rahim2312
Re: Converting Double to Int
Quote:

Originally Posted by Tolls
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.
• 05-01-2012, 05:05 PM
Rahim2312
Re: Converting Double to Int
Quote:

Originally Posted by Tolls
This line:
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:
Code:

`      JOptionPane.showMessageDialog("The distance is " + oneNumber);`
• 05-01-2012, 05:17 PM
Tolls
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.
• 05-02-2012, 03:16 PM
Rahim2312
Re: Converting Double to Int
I am copying this code:
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
• 05-02-2012, 06:58 PM
Tolls
Re: Converting Double to Int
That's saying you don't have a 'frame' variable, so presumably you don't.