I have this assignment for school where I have to use JOptionPane
to calculate the area of different shapes, but when I use JOptionPane.showMessageDialog
, it turns red and the error code says that void
cannot be converted to int
. Can someone help me figure out how to use JOptionPane.showMessageDialog
?
public static void areaTriangle(int base, int height)
{
System.out.println(0.5 * base * height);
}
public static void areaCircle(int radius, double area)
{
area = Math.PI * (radius * radius);
System.out.println(area);
}
public static void areaRectangle(int length, int width)
{
System.out.println(length * width);
}
public static void calcArea()
{
System.out.println("What type of area do you want to calculate?");
inputStr = JOptionPane.showInputDialog("Type 1 for triangle, 2 for circle, 3 for rectangle, and 0 for none, then press ENTER.");
int type = Integer.parseInt(inputStr);
if (type == 0)
{
return;
}
else if (type == 1)
{
inputStr = JOptionPane.showInputDialog("Enter the measurement of the base, then press ENTER.");
int base = Integer.parseInt(inputStr);
inputStr = JOptionPane.showInputDialog("Enter the measurement of the height, then press ENTER.");
int height = Integer.parseInt(inputStr);
int areaT = areaTriangle (base, height);
JOptionPane.showMessageDialog(null, "The area of the triangle is: " + areaT);
}
else if (type == 2)
{
inputStr = JOptionPane.showInputDialog("Enter the measurement of the radius, then press ENTER.");
int radius = Integer.parseInt(inputStr);
double area = Math.PI * (radius * radius);
int areaC = areaCircle (radius, area);
JOptionPane.showMessageDialog(null, "The area of the cicle is: " + areaC);
}
else if (type == 3)
{
inputStr = JOptionPane.showInputDialog("Enter the measurement of the length, then press ENTER.");
int length = Integer.parseInt(inputStr);
inputStr = JOptionPane.showInputDialog("Enter the measurement of the width, then press ENTER.");
int width = Integer.parseInt(inputStr);
int areaR = areaRectangle(length, width);
JOptionPane.showMessageDialog(null, "The area of the rectangle is: " + areaR);
}
}
public static void main(String[] args)
{
calcArea();
}
Your problem has nothing to do with the JOptionPane
and everything to do with your area*
methods
First you declare all your methods as void...
public static void areaTriangle(int base, int height) {
System.out.println(0.5 * base * height);
}
public static void areaCircle(int radius, double area) {
area = Math.PI * (radius * radius);
System.out.println(area);
}
public static void areaRectangle(int length, int width) {
System.out.println(length * width);
}
But then you try and assign a return result to a variable when you call them...
int areaT = areaTriangle(base, height);
//...
int areaC = areaCircle(radius, area);
//...
int areaR = areaRectangle(length, width);
Which simply makes no sense.
Based on what you seem to be trying to do, you need to change your area*
methods to return a value of some kind...
public static double areaTriangle(int base, int height) {
return 0.5 * base * height;
}
public static double areaCircle(int radius, double area) {
return Math.PI * (radius * radius);
}
public static int areaRectangle(int length, int width) {
return length * width;
}
Then you can assign the return values from the methods to some variables...
double areaT = areaTriangle(base, height);
//...
double areaC = areaCircle(radius, area);
//...
int areaR = areaRectangle(length, width);