0

I am getting an error when I try to run this program as a command line argument in Eclipse. This is what the error says:

Error: Main method not found in class, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application

When I change the main method to String then I can't call on the second method as the String main method is not compatible with the int returnSum method.

I need to have the returnSums method be a int type method as per the requirements but I can't figure out how to do this without getting an error. It says in the requirements that i need to use variable number of arguments for the method but have a hard time grasping the idea.

Can someone please help me with this? Here's my code:

public static void main(int[] args) {

    // Printing the entered digits
    System.out.print("Passing");
    System.out.print(" [ ");

    for (int nums = 0; nums < args.length; nums++) {
        System.out.print(args[nums] + " ");

    }
    System.out.print("] ");
    System.out.println("\nSum is " + returnSum(args)); // Calling on the second method for the sum

}
// Taking the nums from the main method as arguments 
public static int returnSum(int...args) {
    int sum = 0;
    // Calculating the sum
    for (int nums = 0; nums < args.length; nums++) {
        sum = sum + nums;
    }
    return sum;

}

Thank you!

AvenNova
  • 337
  • 2
  • 6
  • 16
  • You need to learn how to parse a string into a number. – SLaks Jun 10 '16 at 19:49
  • See for instance http://stackoverflow.com/questions/5585779/converting-string-to-int-in-java – hcs Jun 10 '16 at 19:50
  • You can't have int[] args. It must be String[] args. Command line arguments are Strings, which you may then have to parse as ints later. – ManoDestra Jun 10 '16 at 19:53
  • Is this all your code? Surely there is a surrounding class definition? For a small sample like this, better to provide all of your code to get more helpful answers for you. – jewelsea Jun 10 '16 at 20:21

4 Answers4

4

Try Below Code:

public class SumOfNumbers {
    public static void main(String[] args) {

        int[] intArgs = new int[args.length];
        for (int x = 0; x < args.length; x++) {
            if (args[x].matches("\\d+")) {
                intArgs[x] = Integer.parseInt(args[x]);
            } else {
                System.out.println(args[x] + " is not a Integer hence skiped in this program");
            }
        }

        // Printing the entered digits
        System.out.print("Passing");
        System.out.print(" [ ");

        for (int nums = 0; nums < intArgs.length; nums++) {
            System.out.print(intArgs[nums] + " ");

        }
        System.out.print("] ");
        System.out.println("\nSum is " + returnSum(intArgs)); // Calling on the second method for the sum

    }

    // Taking the nums from the main method as arguments
    public static int returnSum(int... args) {
        int sum = 0;
        // Calculating the sum
        for (int nums = 0; nums < args.length; nums++) {
            sum = sum + args[nums];
        }
        return sum;
    }
}
Sanjay Madnani
  • 803
  • 7
  • 16
3

@Sanjay: If you will try with argument: 10, 20 , 30 you will get below output:

10, is not a Integer hence skiped in this program
, is not a Integer hence skiped in this program
Passing [ 0 20 0 30 ] 
Sum is 50

10, should not be ignored only , should be ignored. also it intArgs should be of size 3 including 10 or size 2 excluding 10 it should not be of size 4.

Passionate
  • 103
  • 7
1

Change your main method's parameter back to String[], as this is a required signature.

In your main method, parse each argument into an int:

int[] intArgs = new int[args.length];
for(int x=0; x<args.length; x++)
    intArgs[x] = Integer.parseInt(args[x]);

Note that if you pass no arguments or ones that are not integers, this will break. If you want to handle this, you can check for args!=null before and catch a NumberFormatException around this code.

Zircon
  • 4,677
  • 15
  • 32
1

You will need to have your main method signature be (String[] args) but you can loop though the args array in your code, convert each element to an int using Integer.parseInt(), store them in a new array (of type int[]), and send this new array to returnSum.

user2121620
  • 678
  • 12
  • 28