-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeather Fetcher.java
More file actions
31 lines (24 loc) · 933 Bytes
/
Weather Fetcher.java
File metadata and controls
31 lines (24 loc) · 933 Bytes
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
import java.io.*;
import java.net.*;
import java.util.*;
public class WeatherApp {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter city name: ");
String city = sc.nextLine();
try {
// Public free API (no key needed)
String api = "https://wttr.in/" + city + "?format=%C+%t";
URL url = new URL(api);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
BufferedReader br = new BufferedReader(
new InputStreamReader(con.getInputStream())
);
String result = br.readLine();
System.out.println("Weather in " + city + ": " + result);
} catch (Exception e) {
System.out.println("Unable to fetch weather. Check internet or city name.");
}
}
}