Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calculator
#1
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".
#2
Please help, I need this soon, very soon. It is supposed to calculate the area of a triangle.
#3
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]
My Blog | My Setup | My Videos | Have a wonderful day.
#4
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-) -->
#5
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!
My Blog | My Setup | My Videos | Have a wonderful day.


Forum Jump:


Users browsing this thread: 1 Guest(s)