If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Threaded View

Josephus Problem, problem.

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

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.