Inspiration
I saw the challenge on LHD: Share and decided to take on the challenge.
What it does
You input in a state and the program gives its temperature and windspeed. After that, the program determines whether it's a good day to go outside or not.
How we built it
I built it using Java.
Challenges we ran into
I encountered many challenges with parsing the JSON file from the API.
Accomplishments that we're proud of
Getting over those challenges.
What we learned
API's are very interesting to work with
What's next for Weather App
I might add more information that it will give when you input a location.
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Scanner;
import org.json.JSONArray;
import org.json.JSONObject;
public class WeatherApp {
public static void main(String[] args) {
System.out.println("Hello there! Type in a location (all lowercase please) to get all weather-related information about the place!");
Scanner scanner = new Scanner(System.in);
String location = scanner.nextLine();
String realLocation = location.replace(" ", "%20");
System.out.println(location.toUpperCase() + ":");
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder().uri(URI.create("https://api.openweathermap.org/data/2.5/weather?appid=e4714b95ae8eb1ffbLOL7c32316b0108c7&q=" + realLocation)).build();
client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenApply(WeatherApp::parse)
.join();
scanner.close();
}
public static String parse(String responseBody) {
JSONObject wholeAPI = new JSONObject(responseBody);
JSONObject weatherInfo = wholeAPI.getJSONObject("main");
JSONObject windInfo = wholeAPI.getJSONObject("wind");
double kelvinTemp = weatherInfo.getDouble("temp");
double trueTemp = (kelvinTemp - 273.15) * 1.8 + 32; // use this temperature value (in Fahrenheit)
double feelsLikeTemp = weatherInfo.getDouble("feels_like");
double trueFeelsTemp = (feelsLikeTemp - 273.15) * 1.8 + 32; // use this temperature value (in Fahrenheit)
double speedOfWind = windInfo.getDouble("speed") * 2.237;
System.out.println("Temperature: " + trueTemp + "\n" + "Feels like: " + trueFeelsTemp + "\n" + "Wind speed: " + speedOfWind + " mph");
if(trueTemp < 50) {
System.out.println("Seems like it's a bit too cold outside. Maybe try going outside a little later or the next day.");
}
else {
System.out.println("Lovely day to go outside! Why don't you go have a nice walk?");
}
return null;
}
}
Log in or sign up for Devpost to join the conversation.