-1
package CS1301;

public class Homework4 {

    public static void Questions(int value) {
        if (value > 0) {
            int count = 1;

            while (count <= value) {
                if (value % count == 0) {
                    System.out.println(count);
                }  

                count = count ++;
            }
        }
    }
}

So I have been trying to get this to work for a bit now and I cant seem to find what is I'm doing wrong. When I try to run it it says "no main methods found, but I though the method defined by public static void. Also the purpose of this was to find the factors of the value. This is my first program so any pointers on better formatting would also be appreciated.

Jacob G.
  • 28,856
  • 5
  • 62
  • 116

2 Answers2

4

You must have a public static void method called main, which accepts an array of arguments;

package CS1301;

public class Homework4 {

    public static void Questions(int value) {
        if (value > 0) {
            int count = 1;

            while (count <= value) {
                if (value % count == 0) {
                    System.out.println(count);
                }  

                count = count ++;
            }
        }
    }

    public static void main(String[] args) {
        Questions(5);
    }
}
Jack Wilsdon
  • 6,706
  • 11
  • 44
  • 87
Coder
  • 1,175
  • 1
  • 12
  • 32
  • You're using the incorrect parameter for the `main` method. – Jacob G. Sep 23 '17 at 20:35
  • Alright I got it to work, but I wanted to ask what exactly 'public static void' meant in the context I used it in originally. Do I actually need it? Also, how do I use the ++ operator correctly? – user8662311 Sep 23 '17 at 21:23
  • 1
    The duplicate question explain what each of those words meant. The ++ operator is a separate question (which has probably already been answered about 25 times on this site). – Joe C Sep 23 '17 at 21:30
  • 1
    @user8662311 Check out [public static void main meaning](https://stackoverflow.com/questions/29276917/what-does-public-static-void-main-args-mean?noredirect=1&lq=1) and [++ meaning](https://stackoverflow.com/questions/2371118/how-do-the-post-increment-i-and-pre-increment-i-operators-work-in-java) – Coder Sep 23 '17 at 21:31
2

In java, to be able to launch something, you need a particular type of function, which can be recognize at the start point of the code

  • it have to be a void method
  • its name is main
  • its parameter have to be an array of String (used in command-line)

Like this :

public static void main(String[] args){
    // some code like computation
    // or function calls 
}

Further details at Oracle Doc


So you have two option, put your code inside this method, or inside an another one :

public static void questions(int value){
    // ... your code
}

public static void main(String[] args){
    questions(2);
    questions(19);
}

OR

public static void main(String[] args){
    int value = 5;                     // <-- and put you value here
    if (value > 0) {
        int count = 1;
        while (count <= value) {
           //...
        }
    }
}
  • first way is preferable your clarity
  • better respect conventions and start naming variables and methods name with lover case Questions -> questions
azro
  • 53,056
  • 7
  • 34
  • 70