Java Reference
In-Depth Information
32
System.out.println("proximity, and I'll tell you where");
33
System.out.println("that zip code is located, along");
34
System.out.println("with a list of other zip codes");
35
System.out.println("within the given proximity.");
36
System.out.println();
37
}
38
39
// Searches for the given string in the input file; if found,
40
// returns the coordinates; otherwise returns (0, 0)
41
public static String find(String target, Scanner input) {
42
while (input.hasNextLine()) {
43
String zip = input.nextLine();
44
String city = input.nextLine();
45
String coordinates = input.nextLine();
46
if (zip.equals(target)) {
47
System.out.println(zip + ": " + city);
48
return coordinates;
49
}
50
}
51
// at this point we know the zip code isn't in the file
52
// we return fictitious (no match) coordinates
53
return "0 0";
54
}
55
56
// Shows all matches for the given coordinates within the
57
// given number of miles
58
public static void showMatches(String targetCoordinates,
59
Scanner input, double miles) {
60
Scanner data = new Scanner(targetCoordinates);
61
double lat1 = data.nextDouble();
62
double long1 = data.nextDouble();
63
System.out.println("zip codes within " + miles + " miles:");
64
while (input.hasNextLine()) {
65
String zip = input.nextLine();
66
String city = input.nextLine();
67
String coordinates = input.nextLine();
68
data = new Scanner(coordinates);
69
double lat2 = data.nextDouble();
70
double long2 = data.nextDouble();
71
double distance = distance(lat1, long1, lat2, long2);
72
if (distance <= miles) {
73
System.out.printf(" %s %s, %3.2f miles\n",
74
zip, city, distance);
75
}
Search WWH ::




Custom Search