Lexi
Lexi

Reputation: 57

How to get JOptionPane to calculate area?

I have created this code, the GUI pops up and works perfectly, but the area is not being calculated correctly. Any Clue why? I am very new to Java coding, so any help is appreciated. Thanks in advance.

package pkg4.pkg2.pkgnew.project;

import javax.swing.JOptionPane;

public class NewProject {

    public static void main(String[] args) {

        String inputStr = JOptionPane.showInputDialog("Type 1 for the area of Triangle, 2 for area of Circle, 3 for Rectangle, and 0 for area of none of these.");

        int i = Integer.parseInt(inputStr);
        if (i == 1) {
            String input = JOptionPane.showInputDialog("Enter the first value to calculate the area of a triangle: ");
            int n1 = Integer.parseInt(inputStr);
            String inp = JOptionPane.showInputDialog("Enter the second value to calculate the area of a triangle: ");
            int n2 = Integer.parseInt(inputStr);
            areaTriangle(n1, n2);
        }
        if (i == 2) {
            String inpu = JOptionPane.showInputDialog("Enter a value to calculate the area of a circle: ");
            double radius = Integer.parseInt(inputStr);
            areaCircle(radius);
        }
        if (i == 3) {
            String inp = JOptionPane.showInputDialog("Enter the first value to calculate the area of a rectangle: ");
            int m1 = Integer.parseInt(inputStr);
            String inp2 = JOptionPane.showInputDialog("Enter the second value to calculate the area of a rectangle: ");
            int m2 = Integer.parseInt(inputStr);
            areaRectangle(m1, m2);
        } else {
            return;
        }
    }

    public static void areaTriangle(int n1, int n2) {
        int areat = (n1 * n2) / 2;
        JOptionPane.showMessageDialog(null, "The area of a triangle with your values is: " + areat);
    }

    public static void areaCircle(double radius) {
        double areac = Math.PI * (radius * radius);
        JOptionPane.showMessageDialog(null, "The area of a circle with your value is: " + areac);
    }

    public static void areaRectangle(int m1, int m2) {
        int arear = (m1 * m2);
        JOptionPane.showMessageDialog(null, "The area of a rectangle with your values is: " + arear);

    }

    public static void calcArea(int x) {

    }
}

Upvotes: 0

Views: 2443

Answers (1)

RAZ_Muh_Taz
RAZ_Muh_Taz

Reputation: 4099

The problem with your code is that every time you parse the input into a string you are always using the same value of the string. Everytime you call your functions you are using all 1, 2, or 3 for your parameters into your area function calls. So you need to change the Integer.parseInt() to contain the new strings you get from the user like so:

 String input = JOptionPane.showInputDialog("Enter the first value to calculate the area of a triangle: ");
 int n1 = Integer.parseInt(input); //not inputStr <----------
 String inp = JOptionPane.showInputDialog("Enter the second value to calculate the area of a triangle: ");
 int n2 = Integer.parseInt(inp);//not inputStr <---------
 areaTriangle(n1, n2);

Upvotes: 2

Related Questions