-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeatherInformationSystem.java
More file actions
105 lines (91 loc) · 4.57 KB
/
Copy pathWeatherInformationSystem.java
File metadata and controls
105 lines (91 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Scanner;
public class WeatherInformationSystem {
public static void main(String[] args) {
String city = "";
String apiKey = "32ecb285ca54c7666cb9bf7177dc3feb"; // API key
Scanner sc = new Scanner(System.in);
while(true)
{
try {
if (System.getProperty("os.name").contains("Windows")) {
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
} else {
new ProcessBuilder("clear").inheritIO().start().waitFor();
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
System.out.println("\t||\t\tWEATHER FORECAST SYSTEM\t\t||\n");
System.out.print("\t Enter 'q' to quit, or name of any City: ");
city = sc.nextLine();
if(city.equals("q"))
{
sc.close();
System.out.println("\n\t\t\t\tGood Bye!!\n");
break;
}
try {
URI uri = new URI("http", "api.openweathermap.org", "/data/2.5/weather", "q=" + city + "&appid=" + apiKey, null);
java.net.URL url = uri.toURL();
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
// Parse JSON response
String jsonResponse = response.toString();
String weatherDescription = parseWeatherDescription(jsonResponse);
double temperature = parseTemperature(jsonResponse);
int humidity = parseHumidity(jsonResponse);
double windSpeed = parseWindSpeed(jsonResponse);
System.out.println("\n\t\tWeather in " + city + ": " + weatherDescription);
System.out.println("\t\tTemperature: " + temperature + "°C");
System.out.println("\t\tHumidity: " + humidity + "%");
System.out.println("\t\tWind Speed: " + windSpeed + " m/s");
System.out.println("\n\t\t\tPress any key to continue");
city = sc.nextLine();
} else {
System.out.println("Error: Failed to fetch weather data. Response code: " + responseCode);
}
connection.disconnect();
} catch (URISyntaxException | IOException e) {
e.printStackTrace();
}
}
}
private static String parseWeatherDescription(String jsonResponse) {
int startIndex = jsonResponse.indexOf("\"description\":\"") + "\"description\":\"".length();
int endIndex = jsonResponse.indexOf("\"", startIndex);
return jsonResponse.substring(startIndex, endIndex);
}
private static double parseTemperature(String jsonResponse) {
int startIndex = jsonResponse.indexOf("\"temp\":") + "\"temp\":".length();
int endIndex = jsonResponse.indexOf(",", startIndex);
String tempString = jsonResponse.substring(startIndex, endIndex);
return Double.parseDouble(tempString) - 273.15; // Convert temperature from Kelvin to Celsius
}
private static int parseHumidity(String jsonResponse) {
int startIndex = jsonResponse.indexOf("\"humidity\":") + "\"humidity\":".length();
int endIndex = jsonResponse.indexOf(",", startIndex);
String humidityString = jsonResponse.substring(startIndex, endIndex).replaceAll("[^0-9]", "");
return Integer.parseInt(humidityString);
}
private static double parseWindSpeed(String jsonResponse) {
int startIndex = jsonResponse.indexOf("\"speed\":") + "\"speed\":".length();
int endIndex = jsonResponse.indexOf(",", startIndex);
String windSpeedString = jsonResponse.substring(startIndex, endIndex);
return Double.parseDouble(windSpeedString);
}
}