BP Forums
Calculator - Printable Version

+- BP Forums (https://bpforums.info)
+-- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=55)
+--- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=56)
+---- Forum: Java (https://bpforums.info/forumdisplay.php?fid=19)
+----- Forum: Programming Help (https://bpforums.info/forumdisplay.php?fid=30)
+----- Thread: Calculator (/showthread.php?tid=776)



Calculator - Aaron Rogers118 - 04-28-2013

I'm slowly learning java, mostly from Brandonio21's videos, and I have run into a problem. I need to know what I need to do with this code:
[code2=java]package howto;

import javax.swing.JOptionPane;

public class Calculator {

public static void main(String[] args)
{
JOptionPane myIO = new JOptionPane();
double inputText = myIO.showInputDialog("Please enter length:");
double lingth = inputText;
double inputText1 = myIO.showInputDialog("Please enter length:");
double width = inputText1;

double area = (lingth * width) / 2;

JOptionPane.showMessageDialog(null, "The area of the triangle is: " + area);
}
}[/code2]
I need help soon because it is for a school project called "How to".


Re: Calculator - Aaron Rogers118 - 05-02-2013

Please help, I need this soon, very soon. It is supposed to calculate the area of a triangle.


Re: Calculator - brandonio21 - 05-02-2013

So your main problem was that you were not converting your variables from String to Double.

When getting information from a JOptionPane, all of the information the user types in is of the String object. So you need to use Double.parseDouble in order to convert the data to a double.

The fixed code is here:
[code2=java]import javax.swing.JOptionPane;

public class calculator {

public static void main(String[] args)
{
JOptionPane myIO = new JOptionPane();
double inputText = Double.parseDouble(myIO.showInputDialog("Please enter length:"));
double lingth = inputText;
double inputText1 = Double.parseDouble(myIO.showInputDialog("Please enter width:"));
double width = inputText1;

double area = (lingth * width) / 2;

JOptionPane.showMessageDialog(null, "The area of the triangle is: " + area);
}
}[/code2]


Re: Calculator - Aaron Rogers118 - 05-06-2013

Its alive. Its alive!! ITS ALIIIIIIVE!!!!! <!-- s:twisted: --><img src="{SMILIES_PATH}/icon_twisted.gif" alt=":twisted:" title="Twisted Evil" /><!-- s:twisted: -->

Just in time to, Thanks! <!-- s8-) --><img src="{SMILIES_PATH}/icon_cool.gif" alt="8-)" title="Cool" /><!-- s8-) -->


Re: Calculator - brandonio21 - 05-06-2013

Aaron Rogers118 Wrote:Its alive. Its alive!! ITS ALIIIIIIVE!!!!! <!-- s:twisted: --><img src="{SMILIES_PATH}/icon_twisted.gif" alt=":twisted:" title="Twisted Evil" /><!-- s:twisted: -->

Just in time to, Thanks! <!-- s8-) --><img src="{SMILIES_PATH}/icon_cool.gif" alt="8-)" title="Cool" /><!-- s8-) -->

You're... you're.. You're INSANE!!!! <!-- s:o --><img src="{SMILIES_PATH}/icon_e_surprised.gif" alt=":o" title="Surprised" /><!-- s:o -->


Hey, no problem!