Hello, everyone. I'm a first-year Computer Science student just taking my first Java class in the summer. We've been assigned to solve the ever-famous Josephus problem. I know that this forum has a previous post on the topic,
HTML Code:
but I don't believe anyone posted the solution to the thread-starter's answer. I've used some of aesoprock00's coding in my own program.

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

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

This is my breakdown for solving the problem:

1. Use Scanner class to have user input two values for N and M.

2. Construct a StringBuffer such that characters from 0 to N-1 are accepted. In this case, the code would be: StringBuffer str = new StringBuffer(316574).

3. Construct an array to hold the character values of the killed off knights.

4. Construct a mechanism for solving the Josephus problem. The mechanism should be capable of calling values from the StringBuffer, store the values of the deleted knights into the array, and also remove the character values of the deleted knights from the StringBuffer. I used a series of "for" loops to achieve this.

Here is my code.

public class JosephusTrial {

    public static void main(String[] args) {
        //Initiates the variables N and M for later use.
	int N = 0;
	int M = 0;

	//Initiates index variable to use in later "for" loop.
	int index = 0;

        //Inputs Scanner object.
        Scanner cmd = new Scanner(System.in);

        //Asks user for input regarding the number of knights.
	System.out.println("How many knights will be participating?");
        String people = cmd.nextLine();
        System.out.println("There will be " + people + " knights.");

        //Asks user for input regarding the number of knights
        //that must touch the potato before elimination.
        System.out.println("Who will be the last person to touch the ball?");
        String touch = cmd.nextLine();

        System.out.println("Every " + touch + "nd " +
                "person to touch the ball " + "will die.");

	//Converts String inputs to integers.
	N = Integer.parseInt(people);
        M = Integer.parseInt(touch);

	//Initiates Array to store the values of each killed knight.
	int[] deleted = new int[N-1];
	//Creates a new StringBuffer for the program.
	//Since program must work for values 1 to 63,536, I added and set String
        //Buffer capacity equal to total characters of all the numbers: 316,574.
        int buffersize = 316574;
        StringBuffer josephus = new StringBuffer(buffersize);
        //int testbuffersize = josephus.capacity();

        //Creates each knight from N as a separate value.
	for(int i = 0; i < N; i++) {
		josephus.append((char)(i + 1));
        //Starts loops that solves Josephus problem.
        for(int i = 0; i < N-1; i++) {
            if((index + M) > josephus.length()) {
                index = (index + M) % (N);
                //Calls the value of the killed knight from StringBuffer, stores
                //it into Array, and then removes it from StringBuffer
                deleted[i] = (int)josephus.charAt(index);
                josephus.delete(index, (index+1));

        } else{
               index = (index + M);
               deleted[i] = (int)josephus.charAt(index);
               josephus.delete(index, (index+1));
        System.out.println("Therefore, Knight No. " + " is the lone survivor.");
The code is faulty at best. I've only studied Java for a month now and I have a bare-minimum understanding of the concepts I have to use in this program. I know for a fact that a "StringIndexOutOfBoundsException: String index out of range: 8" message will occur for the 3rd line of the else loop. However, my biggest problem is in creating the correct number of "spaces" in the StringBuffer, and then in using the right variables for the "for" loops and type-casting the output of the loops into the array.

Please assist in any way you can. Thank you.