I'm borrowing this post from another forum, because the guy was having troubles very similar to what i'm having, and i'm doing a very similar project. the assignment is one of the Josephus problem, including, from a command line, have a user enter 2 values, 1 for the amount of people, and the other for the number of people to pass. the Josephus problem is:

N people, numbered 0 to N-1, are sitting in a circle. Starting at person 0, a hot potato is passed. After M people touch the potato, the person holding the potato is eliminated, the circle closes ranks, and the game continues with the person sitting after the eliminated person starting with the potato. The last person remaining wins.

Write a program to accept two command line inputs representing N (the number of people in the circle) and M (the number of people required to touch the hot-potato before a person is eliminated). Initialize a StringBuffer to consist of the characters numbered 0 through N-1 (note: since there are 216 = 65,536 Unicode characters your program will be capable of solving Josephus' Problem up to this value of N).

I've got my code to where I think it should at least work with figuring out the last person left after the entire Josephus problem is carried out, but I keep getting out of bounds compile-time errors whenever I run my code, like the following:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException:String index out of range

Here is my code thus far:

import java.lang.StringBuffer;
public class Josephus {
	public static void main (String[] args) {
		//These two variables store the two values for the people and the passes.
		int num = 0;
		int pass = 0;
		//This variable stores the value of the current index.
		int index = 0;
		//These two variables store the command line inputs.
		String N = args[0];
		String M = args[1];
		//Output the command line inputs.
		System.out.println("There will be " + N + " people.");
		System.out.println("A person will be eliminated for every " + M + " touches.");
		//Convert the String command line inputs to integers.
		Integer tmpN = Integer.valueOf(N);
		num = tmpN.intValue();
		Integer tmpM = Integer.valueOf(M);
		pass = tmpM.intValue();
		//Array stores the deleted values.
		int[] deleted = new int[num];
		//Create a new StringBuffer for the program.
		StringBuffer josephus = new StringBuffer(num);
		//Create the values needed for the program.
		for(int i=0; i<num; i++) {
		//Test garbage.
		//Loop to solve the Josephus problem.  Note the -1 may need changed.
		for(int i=0; i><num-1; i++) {
			if((index+pass) > (num)) {
				index = (index + pass) % (num);
				//Get the value from the StringBuffer, then delete the value.
				deleted[i] = (int)josephus.charAt(index);
				josephus.delete(index, index+1);
			} else {
				index += pass;
				deleted[i] = (int)josephus.charAt(index);
				josephus.delete(index, index+1);
		System.out.println("The final remaining member is " + (int)josephus.charAt(0));
If anyone could point out where the problems with this could be occurring, I'd appreciate it. If someone could also provide me with some tips for figuring out problems like this on my own more easily, I would definitely appreciate that as well. I've just played around with the numbers for the past few hours with no luck at ever getting it to run successfully.