I think it would be appropriate to use actual objects to represent your points and quadrilaterals.
Using objects can make the code much clearer and easier to test.
package com.stackoverflow.so47518648;
import java.util.Arrays;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@SuppressWarnings("javadoc")
public class Answer {
// Allow user to quit by entering Q or q
static final Pattern SCANNER_QUIT_PATTERN = Pattern.compile("^q$", Pattern.CASE_INSENSITIVE);
// Use of named-capturing groups, in this case 'x' and 'y' requires java 7
static final Pattern SCANNER_INPUT_PATTERN = Pattern.compile("(?<x>\\d+),\\s*(?<y>\\d+)");
public static void main(String... args) {
try (Scanner sc = new Scanner(System.in)) {
Quadrilateral quadrilateral = promptForQuadrilateral(sc);
System.out.println("Finding area for: " + quadrilateral);
double area = quadrilateral.area();
System.out.println("The area of " + quadrilateral + " is :" + area);
} catch (UserQuit userQuit) {
System.out.println(userQuit.getMessage());
}
}
static Quadrilateral promptForQuadrilateral(Scanner sc) {
Point a = promptForPoint(sc, "A");
Point b = promptForPoint(sc, "B");
Point c = promptForPoint(sc, "C");
Point d = promptForPoint(sc, "D");
Quadrilateral q = new Quadrilateral(a, b, c, d);
return q;
}
static Point promptForPoint(Scanner sc, String vertexName) {
while (true) {
System.out.println("Enter the coordinates for " + vertexName + " (x,y) or q to quit: ");
String coordinates = sc.findInLine(SCANNER_INPUT_PATTERN);
if (coordinates != null) {
sc.nextLine();
Matcher matcher = SCANNER_INPUT_PATTERN.matcher(coordinates);
matcher.matches();
int x = Integer.parseInt(matcher.group("x"));
int y = Integer.parseInt(matcher.group("y"));
Point point = new Point(x, y);
System.out.println("For " + vertexName + " you entered: " + point);
return point;
} else if (sc.findInLine(SCANNER_QUIT_PATTERN) != null) {
throw new UserQuit();
} else {
// Reprompt when invalid input is received
sc.next();
}
}
}
public static class UserQuit extends RuntimeException {
public UserQuit() {
super("User quit.");
}
}
public static class DuplicatePoints extends RuntimeException {
public DuplicatePoints(Point... points) {
super("Cannot create quadrilaterl with duplicate points: " + Arrays.toString(points));
}
}
public static class Point {
public final int x;
public final int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
double distance(Point p1, Point p2) {
return Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2));
}
/**
* Distance from this point to the supplied point.
*
* @param that the that
* @return the double
*/
public double distance(Point that) {
return distance(this, that);
}
@Override
public String toString() {
return "(x=" + this.x + ", y=" + this.y + ")";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + this.x;
result = prime * result + this.y;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Point other = (Point) obj;
if (this.x != other.x) {
return false;
}
if (this.y != other.y) {
return false;
}
return true;
}
}
public static class Quadrilateral {
public final Point a;
public final Point b;
public final Point c;
public final Point d;
public Quadrilateral(Point a, Point b, Point c, Point d) {
validatePoints(a, b, c, d);
this.a = a;
this.b = b;
this.c = c;
this.d = d;
}
static void validatePoints(Point a, Point b, Point c, Point d) {
// throw exceptions if we can't construct a quadrilateral (duplicate points, etc)
// Duplicate points check
Point[] points = { a, b, c, d };
if (Arrays.stream(points)
.distinct()
.count() != 4) {
throw new DuplicatePoints(points);
}
}
static double calulateArea(Quadrilateral quadrilateral) {
return calulateArea(quadrilateral.a, quadrilateral.b, quadrilateral.c, quadrilateral.d);
}
static double calulateArea(Point a, Point b, Point c, Point d) {
// do calculation here.
return 0.0;
}
public double area() {
return calulateArea(this);
}
@Override
public String toString() {
return "a=" + this.a + ", b=" + this.b + ", c=" + this.c + ", d=" + this.d;
}
}
}