0

i'm receiving this error with my java application. I believe the fault is with the add function for my array list. Is there a better way of incorporating this idea without going too complex? (First week of java) Thanks a lot guys!

import java.util.ArrayList;
import java.util.Scanner;


public class PrimeSieve {
    public static void main(int[] args) {
        System.out.println("Enter the max integer(N value): ");
        Scanner scan = new Scanner(System.in);
        int num = scan.nextInt();
        System.out.println("Compute prime numbers from 2 to: " + num);
        if(num < 2){
            System.out.println("N must be greater than or equal to 2.");
        }
        else if(num == 2) {
            System.out.println("Prime numbers: 2");
        }
        else {
            ArrayList<Integer> myArr = new ArrayList<Integer>(); 
            int i = 2;
            while(i <= num){
                myArr.add(i);
            }
            System.out.println(myArr);
        }
    }
}

The error -

Exception in thread "main" java.lang.NoSuchMethodError: main
1
  • I'm rolling this back to its original form. The edit you put in essentially deleted meat of the question. For the sake of future visitors, it's better to just keep the question in its original form, and let the answers below provide the answer. You should also accept the one that helped you most. Commented Feb 4, 2014 at 0:51

2 Answers 2

7

Declare it as public static void main(String[] args)

That will fix it. You currently have (int[] args).

Note that this is not a while loop exception, it is an error
generated at runtime as no proper main method can be found.

See the JLS 12.1.4.: http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.1.4

Sign up to request clarification or add additional context in comments.

2 Comments

What I don't understand is the fact that i'm trying to make an arraylist that only holds integers. Wouldn't String[] create an array list that holds strings? Also, do you think you could help me with the Else function as it's not correctly functioning. I'm trying to make a list with integers from 2 to num. (num is the user input #) Thank you.
@user2908101, please see my response below as there is a way to parse String[] args such that the method can interpret each as an int.
2

The problem is that the signature of the main method MUST be:

public static void main(String[] args)

You are using int[] args, which unfortunately, is not allowed nor recognized in the main method.

If you really want to interpret the arguments as integers, you can type Integer.parseInt( args[...] ) wherever needed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.