Nathan Limmer
Nathan Limmer

Reputation: 11

Intellij Could not find class com.company.Main

This code is supposed to identify prime numbers. This is done by going one integer at a time and removing integers that are factors of the one selected. This continues until the "max" integer is reached that the user types in. I am new to Intellij and Java, so I am not sure how to name the class or what it should be.

package com.company;
    class Sieve
    {
        int max;
        boolean numbers[];
        public Sieve(int max)
        {
            this.max = max;
            boolean[] numbers = new boolean[max];
            if (max < 2)
            {
                throw new IllegalArgumentException();
            }  // End of if
            numbers[0] = false;
            numbers[1] = false;
        } // End of Sieve method

        public void findPrimes()
        {
            for (int i = 0; i < max; i++)
            {
                if (numbers[i])
                {
                    int j = 2*i;
                    while (j < max)
                    {
                        numbers[j] = false;
                        j = j+i;
                    }
                }
            }
        }

        public String toString()
        {
            String z = "";
            for (int i = 0; i < max; i++)
            {
                if (numbers[i])
                {
                    z = z + i + " ";
                }
            }
            return z;
        }
        void main() {

        }
    }

Upvotes: 1

Views: 1518

Answers (2)

Jonnathan Q
Jonnathan Q

Reputation: 312

Your class name is Sieve you have to configure IntelliJ to execute from that class and not from Main (this maybe got configured by default), by other hand make sure to declare the main function like this:

public static void main(String args[]){
    //your code here...
}

That is the entry point of your program so put your logic inside it, also declare your class Sieve as public:

public class Sieve...

I suggest you to read more about IntelliJ at https://www.jetbrains.com/help/idea/creating-and-running-your-first-java-application.html

Upvotes: 1

Sky
Sky

Reputation: 36

There is no convention in class naming, just chose a name clear enough and use it, I always ask myself if I come back after a month and read my code is this class name, variable name, function name will help me remember/understand my code. And of course I always have some comments to complete the explanation, you can't rely on names only.

Upvotes: 1

Related Questions