-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGetWeather.java
More file actions
36 lines (30 loc) · 1.56 KB
/
GetWeather.java
File metadata and controls
36 lines (30 loc) · 1.56 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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
public class GetWeather
{
public static void main(String[] args) throws Exception {
System.out.println(getWeather("Seattle"));
}
public static String[] getWeather(String place) throws Exception {
URL url = new URL("https://api.openweathermap.org/data/2.5/weather?q=" + place + "&appid=43eb0b382279761afe085d3c6a5dcfc7");
URLConnection connection = url.openConnection();
try (BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream())))
{
String line = in.readLine();
double kelvinTemp = Double.parseDouble(line.substring(line.indexOf("p\":") + 3, line.indexOf(",\"f")));
int farenheitTemp = (int) ((kelvinTemp - 273.15) * (9/5) + 32);
String temperature = String.valueOf(farenheitTemp) + " degrees";
String humidity = line.substring(line.indexOf ("humidity") + 10, line.indexOf("visibility") - 3);
if (humidity.indexOf("sea_level") != -1) {
humidity = line.substring(line.indexOf("humidity") + 10, line.indexOf("visibility") - 37);
}
String windSpeed = line.substring(line.indexOf ("speed") + 7, line.indexOf("deg") - 2);
String weatherCondition = line.substring(line.indexOf("on\":\"") + 5, line.indexOf("\",\"i"));
String[] results = {temperature, weatherCondition, humidity, windSpeed};
return results;
}
}
}