Reputation: 337
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!
Upvotes: 0
Views: 611
Reputation: 103
@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.
Upvotes: 3
Reputation: 813
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;
}
}
Upvotes: 4
Reputation: 676
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
.
Upvotes: 1
Reputation: 4707
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.
Upvotes: 1