In: Computer Science
METAR Abbreviations
SPECI KPWK 042100Z 05010KT 10SM BKN030 OVC036 M01/M08 A3049 RMK AO2 T10111078Translation:
METAR KMDW 070551Z 26013G17KT 10SM FEW020 M23/M27 A3024 RMK AO2 SLP276 4/010 T12331272 11228 21239 411831261 55003 $
Project 1 directions:
+ - B DZ E HZ RA SN
import java.util.*;
import java.lang.*;
import java.io.*;
class Project
{
public static void main (String[] args) throws
java.lang.Exception
{
Scanner sc = new
Scanner(System.in);
String wReport = sc.nextLine();
//input should be given in a single line space seperated
String[] codes = wReport.split("
"), preCodes = {"DZ", "HZ", "RA", "SN"};
String[] preCodeMeanings =
{"drizzle", "haze", "rain", "snow"}; //could have also used hashmap
rather than array
int l = codes.length,
preCodesLength = preCodes.length;
for(int i=0;i<l;i++){
System.out.print(codes[i]+": ");
String code =
codes[i], subCode = code, ans = "";
boolean flag =
false;
if(code.length()
== 1){
System.out.println("Unknown
abbreviation");
continue;
}
if(code.charAt(0) == '+' || code.charAt(0) == '-'){
if(code.charAt(0) == '+')ans+="heavy";
else ans+="light";
subCode = code.substring(1);
}
int j = 0;
for(j=0;j<preCodesLength;j++){
String
subSubCode = subCode.substring(0,2);
if(subSubCode.equals(preCodes[j])){
if(ans.length()>0)ans+=" "+preCodeMeanings[j];
else
ans+=preCodeMeanings[j];
break;
}
}
if(j < preCodesLength){
flag =
true;
}
if(subCode.length()>2){
if(!(ans.equals("light") || ans.equals("heavy")) && flag ==
true){
if(subCode.charAt(2) == 'B')ans+=" "+"began at
time";
else if(subCode.charAt(2) == 'E')ans+=" "+"ended
at time";
if(subCode.length() > 3){
String subSubCode =
subCode.substring(3);
if(subSubCode.length()>0){
ans+="
"+subSubCode;
if(subCode.charAt(2) == 'B')ans+=" minutes after the top of the
last hour";
else
ans+=" minutes after the top of the last hour";
System.out.println(ans);
continue;
}
}
}
}
else if(flag == true){
System.out.println(ans);
}
System.out.println("Unknown
abbreviation");
}
}
}
Custom Input: KMSP 220853Z 36013KT 9SM -RAB5 FEW018 BKN030 OVC042 M03/M07 A3035 RMK AO2 SLP288
Output:
KMSP: Unknown abbreviation 220853Z: Unknown abbreviation 36013KT: Unknown abbreviation 9SM: Unknown abbreviation -RAB5: light rain began at time 5 minutes after the top of the last hour FEW018: Unknown abbreviation BKN030: Unknown abbreviation OVC042: Unknown abbreviation M03/M07: Unknown abbreviation A3035: Unknown abbreviation RMK: Unknown abbreviation AO2: Unknown abbreviation SLP288: Unknown abbreviation