I'm new to Java but have one college semester of C under my belt. My IDE is NetBeans 6.5, which I really like. I'm trying to write a function that converts a number in DegMinSec format (sexagesimal) to decimal degrees in order to perform trig functions (sin, cos, tan) on it. I have copied and pasted the code that I have come up with so far. I changed the input (double deg_min_sec) several times to check the results but I haven't been 100 % successful. My post contains the output below the code so you can see what's going on. Any suggestions would be greatly appreciated! FYI, I'm not a college student looking for someone to do my homework. I just do this as a hobby.
Regards,
Mark
package dms_to_dd;
/*
* Date: 12 June 2009
* Author: neophyte
* Purpose: Convert DMS to DD
*/
public class DMS_to_DD {
public static void main(String[] args) {
int degrees = 0; /* integer part DMS */
double deg_min_sec = 36.5212; /* DMS */
double minutes_seconds = 0; /* fractional part DMS */
double minutes = 0, seconds = 0; /* minutes, seconds DD */
degrees = (int) deg_min_sec;
minutes_seconds = deg_min_sec - degrees;
String min_sec = Double.toString(minutes_seconds);
String min = min_sec.substring(2,4); /* minutes */
String sec = min_sec.substring(4,6); /* seconds */
minutes = Double.valueOf(min) / 60; /* minutes DD */
seconds = Double.valueOf(sec) / 3600; /* seconds DD */
System.out.format("%.4f%n", degrees + minutes + seconds);
}
}
----------------------------------------double deg_min_sec = 36.5212;
run:
36.8700 /* correct */
BUILD SUCCESSFUL (total time: 0 seconds)
-----------------------------------------
double deg_min_sec = 90.0000;
run:
Exception in thread "main"
java.lang.StringIndexOutOfBoundsException:
String index out of range: 4
at java.lang.String.substring(String.java:1935)
at dms_to_dd.DMS_to_DD.main(DMS_to_DD.java:21)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)
-----------------------------------------
double deg_min_sec = 0.2015;
run:
0.3375 /* correct */
BUILD SUCCESSFUL (total time: 0 seconds)
-----------------------------------------
double deg_min_sec = 90.0006;
run:
90.0000 /* wrong! */
BUILD SUCCESSFUL (total time: 2 seconds)
Edited by WingedPanther, 20 June 2009 - 09:06 AM.
add code tags (the # button)


Sign In
Create Account

Back to top










