public class LongLat {
public static void main(String args[]){
double LongDouble=0;
double direction=0;
int checkDegree=0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input=null;
System.out.println("Enter Longitude or Latitude");
try{
input=br.readLine().toUpperCase();
}catch(Exception e){
System.out.println("IO error");
System.exit(1);
}
Pattern p=Pattern.compile("[NEWS]");
Matcher m=p.matcher(input);
if(input.contains("N") || input.contains("E"))
direction=1;
else if(input.contains("W") || input.contains("S"))
direction=-1;
if(input.contains("N") || input.contains("S"))
checkDegree=90;
else if(input.contains("W") || input.contains("E"))
checkDegree=180;
input=m.replaceFirst("");
input=input.trim();
String[] LongDMS=input.split(" ");
if(!(Integer.parseInt(LongDMS[0])<=checkDegree)){
System.out.println("Degree should be less than "+checkDegree);
System.exit(1);
}
if(!(Integer.parseInt(LongDMS[1])<=60)){
System.out.println("Minutes should be in between 0 and "+60);
System.exit(1);
}
if(!(Integer.parseInt(LongDMS[2])<=60)){
System.out.println("Second should be in between 0 and "+60);
System.exit(1);
}
if(LongDMS.length==3)
LongDouble=Double.valueOf(LongDMS[0])+((Double.valueOf(LongDMS[1])*60)+Double.valueOf(LongDMS[2]))/3600;
LongDouble=LongDouble*direction;
System.out.println(LongDouble);
}
}
Enter Longitude or Latitude
W87 43 41
-87.72805555555556
No comments:
Post a Comment