1
package src.sheet1.question1;

class OneFourTwoOne {

    public static void main(String[] args) {

        assert args.length == 1;

        int x = Integer.parseInt(args[0]);

        System.out.print(x);

        while(x != 1) {
            x = next(x);
            System.out.print(" " + x);
        };

    }

    static int next(int x) {

        return ((x % 2) == 0) ? (x / 2) : (3*x + 1);

    }

}

when I input :java OneFourTwoOne in the terminal, here occurs an error: Error: Could not find or load main class OneFourTwoOne How can I run it?

0

2 Answers 2

3

Java cannot run your class because it is in a package. Java expects the package name to be matched by the path to your file.

Create a directory structure that looks like this:

src
  +-sheet1
     +-question1
        +-OneFourTwoOne.java

and compile your class into OneFourTwoOne.class. Now switch the parent directory of src, and execute the command

java src.sheet1.question1.OneFourTwoOne
Sign up to request clarification or add additional context in comments.

Comments

0

Java cannot run your class because it is in a package. Java expects the package name to be matched by the path to your file.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.