Search code examples
javacompilationjosephus

Java syntax issues


I'm trying to do a Josephus problem, but I'm not allowed to use code snippets from other people. With this in mind, I have 27 errors in my code, but can't figure out how to fix it. Would you wonderful people explain to me why it won't compile. I want to see if my logic is flawed, but I can't test it because it won't compile. Any other tips are more than welcome! Here is my code:

import java.util.*;

public class Josephus
{
    public class Link
    {
        public int num;
        public Link next;

        public Link (int d)
        {
            num = d;
            next = null;
        }
    }

    public class Main
    {

        Scanner in = new Scanner(System.in);
        System.out.println("How many players");

        int numPlayers = in.nextInt();
        Link first, last;
        first = last = new Link (1);

        for(int k=2; k<=numPlayers; k++)
        {
            last.next = new Link(k);
            last = last.next;
        }
        last.next = first;

        System.out.println("How many skips");
        int m = in.nextInt();

        for (int g=0; g<numPlayers; g++)
        {
            for (int k=0; k<=m; k++);
            {
                last = last.next;
            }
            last.next;
            last = last.next;
        }
    }
}

Solution

  • I cleaned up a bit your code so it compiles, you might be better off with two classes

    Josephus.java

    import java.util.Scanner;
    
    public class Josephus {
    
    public static void main(String[] args) {
    
        Scanner in = new Scanner(System.in);
        System.out.println("How many players");
    
        int numPlayers = in.nextInt();
        Link first = new Link(1);
        Link last = new Link(1);
    
        for (int k = 2; k <= numPlayers; k++) {
            last.next = new Link(k);
            last = last.next;
        }
        last.next = first;
        first.next = last;
    
        System.out.println("How many skips");
        int m = in.nextInt();
    
        for (int g = 0; g < numPlayers; g++) {
            for (int k = 0; k <= m; k++)
            {
                last = last.next;
            }
            // last.next;
            last = last.next;
        }
    
        in.close();
    }
    }
    

    and Link.java

    public class Link {
    
        public int num;
        public Link next;
    
        public Link(int d) {
            num = d;
            next = null;
        }
    }
    

    This compiles and accepts input, then throws an error. I haven't fixed that since I don't know exactly what you're trying to achieve.

    Changes:

    1. Changed `"class main"` to `public static void main(){...)` The Java runtime will look for this method when it's called.
    2. Extracted class `Link` to it's own file other solutions are possible, like some other answers say, you could declare it as static or instantiate `Josephus` (and probably some other ways)
    3. Commented out the line `last.next`, this doesn't actually solve any problem, it just eliminates the compilation error and allows you to compile, since I don't see what you're trying to do here I couldn't think of a better solution.
    4. It's not necessary to compile but I added the line `in.close();` at the end of the main method to free allocated resources, without this line it'll throw a compile warning.
    5. Added `first.next = last;` to avoid the null pointer exception that was happening.

    All that you should need now is to implement the Josephus logic inside the nested loops and output your results (if desired) all of which should be quite language agnostic.