Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calculator
#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.


Messages In This Thread
Calculator - by Aaron Rogers118 - 04-28-2013, 07:05 PM
Re: Calculator - by Aaron Rogers118 - 05-02-2013, 12:28 PM
Re: Calculator - by brandonio21_phpbb3_import2 - 05-02-2013, 02:38 PM
Re: Calculator - by Aaron Rogers118 - 05-06-2013, 10:05 AM
Re: Calculator - by brandonio21_phpbb3_import2 - 05-06-2013, 02:23 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)