05-02-2013, 02:38 PM
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]
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]