-

-

Sunday, March 25, 2007

Getting lost in code

So this semester I decided to take two different programming classes in two different languages, one in Java and one in VBA for ArcObjects. While I enjoy programming when I can get something to work, it sure can be a frustrating and outright horrible, uncompromising process. Below is a method in the Java language that I just finally got to work (after hours of pain!!!). It rotates any string ( group of characters) represented by msg by the offset amount entered by the user and captured in the first line. The hard part was that the offset could be positive or negative and also much longer than the string itself, in which case it has to wrap around and start again.

public static String strRotateRight ( String msg, int offset ) {
String rightRotate = "";
if ( msg == null)
return rightRotate;
if (msg.length() ==0)
return msg;

if (offset>0){
int newOffset = (offset - (offset/msg.length()*msg.length()));
for (int i = (msg.length()-newOffset); i
rightRotate+=msg.charAt(i);
}
for (int i = 0; i < (msg.length()-newOffset); i++){
rightRotate+=msg.charAt(i);
}

}else if (offset<0){
int newOffset = (offset/msg.length()*msg.length())-offset;
for (int i = newOffset; i
rightRotate+=msg.charAt(i);
}
for (int i = 0; i < newOffset; i++){
rightRotate+=msg.charAt(i);

}
}else{ rightRotate=msg;

}

return rightRotate;
}

2 comments:

rsiegel said...

Nice work dude. Good on ya' for learning the coding-stuff.

Stephen said...

I've been working on that for years and there you go figuring it out in a few weeks! That's it, I'm changing careers.