0
/*
(Geometry: area of a triangle) Write a program that prompts the user to enter
three points (x1, y1), (x2, y2), (x3, y3) of a triangle and displays its area.
*/
import java.util.Scanner;

public class Exercise_02_19 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        // Prompt the user to enter three points
        System.out.print("Enter three points for a triangle: ");
        double x1 = input.nextDouble();
        double y1 = input.nextDouble();
        double x2 = input.nextDouble();
        double y2 = input.nextDouble();
        double x3 = input.nextDouble();
        double y3 = input.nextDouble();

        // Compute the area of a triangle
        double side1 = Math.pow(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2), 0.5);
        double side2 = Math.pow(Math.pow(x3 - x2, 2) + Math.pow(y3 - y2, 2), 0.5);
        double side3 = Math.pow(Math.pow(x1 - x3, 2) + Math.pow(y1 - y3, 2), 0.5);
        double s = (side1 + side2 + side3) / 2;
        double area = Math.pow(s * (s - side1) * (s - side2) * (s - side3), 0.5);

        // Display result
        System.out.println("The area of the triangle is " + area);
    }
}

I am using Java, how can I convert double x1 = input.nextDouble(); into an array that does x1 to 3 and y1 to 3? I am currently learning how to use arrays in java but even reading the textbook is hard to understand. Can someone help me with how I can convert this into an array?

zucc0nit
  • 315
  • 1
  • 11

1 Answers1

1
double[] x = new double[3];
double[] y = new double[3];

Now you can access x1 to x3 as x[0] to x[2] and similarly for y.

Abishek Stephen
  • 173
  • 1
  • 1
  • 13
  • Thank you for this. I am just having one more problem, I added this into the java file but now the x2 in the file is saying: `The local variable x1 may not have been initialized` how can I fix this when it's an array? – zucc0nit Sep 13 '21 at 19:37
  • 1
    Change x1 to x[0], x2 to x[1] and x3 to x[2]. What we have done is replace x1, x2 and x3 to the array values of x[0], x[1] and x[2]. Do the same for "y" also. – Abishek Stephen Sep 13 '21 at 19:41
  • Abishek, how can I still ask for user input then give the output of the triangle? Before I used the array's the program worked but now it's not. How can I fix it? – zucc0nit Sep 13 '21 at 20:09
  • Share what error it is showing. I can't help if you do not share the error specifically. – Abishek Stephen Sep 13 '21 at 20:11
  • There is no error, it just shows this: https://i.imgur.com/408e8I7.png No output. – zucc0nit Sep 13 '21 at 20:29
  • You have removed the part where you read the value with input.nextDouble() then the value will not be set right? When I said change x1 to x[0] you deleted the part where the value is input. Do you get my point? – Abishek Stephen Sep 13 '21 at 20:34
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/237066/discussion-between-zucc0nit-and-abishek-stephen). – zucc0nit Sep 13 '21 at 20:37