-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweatherApp.java
More file actions
81 lines (73 loc) · 2.57 KB
/
weatherApp.java
File metadata and controls
81 lines (73 loc) · 2.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
package liveWeather;
import java.net.*;
import java.io.*;
import java.util.*;
import java.util.Map.*;
import java.util.regex.*;
public class weatherApp
{
public static void showWeather(String location){
String key="1516fe41c920442babd110027200105&q="+location;
String url="http://api.weatherapi.com/v1/current.json?key="+key;
try{
URL uh=new URL(url);
BufferedReader bf=new BufferedReader(new InputStreamReader(uh.openStream()));
String json;
json=bf.readLine();
String data[]=json.split(",");
Map<String,String> map=new HashMap<String,String>();
for(int i=1;i<data.length;i++){
if(Pattern.compile(".region*").matcher(data[i]).find()){
data[i]=data[i].substring(data[i].indexOf(":")+1,data[i].length());
map.put("Region",data[i]);
}
else if(Pattern.compile(".country*").matcher(data[i]).find()){
data[i]=data[i].substring(data[i].lastIndexOf(":")+1,data[i].length());
map.put("Country",data[i]);
}
else if(Pattern.compile(".localtime[^a-z]*").matcher(data[i]).find()){
data[i]=data[i].substring(data[i].indexOf(":")+1,data[i].length()-1);
map.put("Localtime",data[i]);
}
else if(Pattern.compile(".temp_c.").matcher(data[i]).find()){
data[i]=data[i].substring(data[i].lastIndexOf(":")+1,data[i].length());
map.put("Temperature(Celcius)",data[i]);
}
else if(Pattern.compile(".*temp_f*").matcher(data[i]).find()){
data[i]=data[i].substring(data[i].lastIndexOf(":")+1,data[i].length());
map.put("Temperature (Fahrenheit)",data[i]);
}
else if(Pattern.compile(".is_day*").matcher(data[i]).find()){
if(data[i].charAt(data[i].length()-1) == '1')
map.put("Day/Night","Day");
else
map.put("Day/Night","Night");
}
else if(Pattern.compile(".*text*").matcher(data[i]).find()){
data[i]=data[i].substring(data[i].lastIndexOf(':')+1,data[i].length());
map.put("Condition",data[i]);
}
else{
continue;
}
}
for(Entry<String,String> e:map.entrySet()){
System.out.println(e.getKey()+" : "+e.getValue()+"\n");
}
}
catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
System.out.print("Enter a city name : ");
@SuppressWarnings("resource")
Scanner kb = new Scanner(System.in);
String location=kb.nextLine();
System.out.println("\n\n\t\t\t\t=======================");
System.out.println("\t\t\t\t|| Live Weather Info ||");
System.out.println("\t\t\t\t=======================\n\n");
System.out.println("Given location : "+location+"\n\n");
showWeather(location);
}
}