First of all, note that you can always use Point2D.Double
for representing points in a plane.
Next, I would split your program into two methods: the first one for pruning points that are not within a specified bounding box, and the second one for reading the points from the standard input, and putting them into the first one.
What comes to coding conventions, I would have a blank line before the if
statement. Also, you should surround the condition of an if
statement with a single space. So, instead of
String[] values = data.split(",", 3);
if(Double.parseDouble(values[0])){ ...
you should write
String[] values = data.split(",", 3);
if (Double.parseDouble(values[0])) { ...
All in all, I had this in mind:
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;
public class CoordinateUtilities {
public static List<Point2D.Double>
getPointsWithinBoundingBox(final List<Point2D.Double> pointList,
final double lowX,
final double highX,
final double lowY,
final double highY) {
Objects.requireNonNull(pointList, "The list of points is null.");
checkXBounds(lowX, highX);
checkYBounds(lowY, highY);
final List<Point2D.Double> ret = new ArrayList<>(pointList.size());
for (final Point2D.Double point : pointList) {
final double x = point.x;
final double y = point.y;
if (lowX <= x && x <= highX && lowY <= y && y <= highY) {
ret.add(point);
}
}
return ret;
}
public static void main(final String... args) {
final Scanner scanner = new Scanner(System.in);
final double lowX = 50.0;
final double highX = 100.0;
final double lowY = 30.0;
final double highY = 60.0;
final List<Point2D.Double> allPoints = new ArrayList<>();
while (scanner.hasNextLine()) {
final String line = scanner.nextLine();
if (line.trim().equals("quit")) {
break;
}
final Point2D.Double point = parsePoint(line);
if (point != null) {
allPoints.add(point);
}
}
final List<Point2D.Double> boundedPoints =
getPointsWithinBoundingBox(allPoints, lowX, highX, lowY, highY);
boundedPoints.forEach(System.out::println);
}
private static Point2D.Double parsePoint(final String line) {
final String[] tokens = line.trim().split("\\s*,\\s*");
if (tokens.length < 2) {
return null;
}
try {
final double x = Double.parseDouble(tokens[0]);
final double y = Double.parseDouble(tokens[1]);
return new Point2D.Double(x, y);
} catch (final NumberFormatException ex) {
return null;
}
}
private static void checkXBounds(final double lowX, final double highX) {
if (Double.isNaN(lowX)) {
throw new IllegalArgumentException(
"The lower bound of the x-coordinate is NaN.");
}
if (Double.isNaN(highX)) {
throw new IllegalArgumentException(
"The upper bound of the x-coordinate is NaN.");
}
if (lowX > highX) {
throw new IllegalArgumentException(
"The lower bound of the x-coordinate (" + lowX + "is " +
"larger than the upper bound of the x-coordinate (" +
highX + ").");
}
}
private static void checkYBounds(final double lowY, final double highY) {
if (Double.isNaN(lowY)) {
throw new IllegalArgumentException(
"The lower bound of the y-coordinate is NaN.");
}
if (Double.isNaN(highY)) {
throw new IllegalArgumentException(
"The upper bound of the y-coordinate is NaN.");
}
if (lowY > highY) {
throw new IllegalArgumentException(
"The lower bound of the y-coordinate (" + lowY + "is " +
"larger than the upper bound of the y-coordinate (" +
highY + ").");
}
}
}
Hope that helps.