Reputation: 334
I'm currently working on a school project in Eclipse (We have just started using this) and we are running into a small hickup: Because we used BlueJ before, we did not have any main method, we added one but ran into a problem.
We have to send an Int value in the parameter instead of a String[] with args, we tried public static void main(Int[] args) This results in the following error:
Error: Main method not found in class Tester, please define the main method as: public static void main(String[] args)
I'm wondering what we should do/change to get this to work.
Upvotes: 3
Views: 4904
Reputation: 5034
Have a look into Integer.parseInt("123")
,
see http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt%28java.lang.String,%20int%29
I hope you'll figure out the rest :)
Upvotes: 8
Reputation: 1864
This class will print out the integers you put in on the command line:
public class IntegersFromCommandLine
{
public static void main(String[] args)
{
for (int i = 0; i < args.length; i++)
{
System.out.println(Integer.parseInt(args[i]));
}
}
}
If you give it the command line arguments 1324 21 458 9564 1 0 -789 40, it will give the following output:
1324
21
458
9564
1
0
-789
40
Upvotes: 1
Reputation: 31
You need to pass your value as a String then parse it as an Integer.
Look this sample of code :
public static void main(String[] args) throws Exception {
int value = Integer.parseInt(args[0]);
System.out.println("My int value: " + value);
}
You need to protect the parseInt with try catch for error management, if parameter is not parsable.
Upvotes: 2
Reputation: 4157
You must leave the main method as follows:
public static void main(String[] args){
//....
}
As you indicate the error. Now, in the main method can change the type of the arguments to integers.
public static void main(String[] args){
int arg0=Integer.parseInt(args[0]);
int arg1=Integer.parseInt(args[1]);
//... etc.
}
Regards!
Upvotes: 1
Reputation: 8540
write a main method like below
public static void main(String[] args){
}
.
and use Integer.parseInt(str)
Upvotes: 1
Reputation: 6633
Try sending your integer in the String array.
new String[]{"1"}
Then fetch it:
Integer yourInteger = Integer.valueOf(args[0])
Upvotes: 2
Reputation: 62459
As others have said, you cannot change the signature of the main
method. To obtain an integer from the String
parameters use:
public static void main(String[] args) {
int first = Integer.parseInt(args[0]); // use whatever index you need
}
Upvotes: 3
Reputation: 2842
Just pass in a string, and then use Integer.parseInt(string)
to get the integer you need back.
Upvotes: 1
Reputation: 8255
Java will look for public static void main(String[] args)
.
You will have to pass the integer value as a String and parse it.
Here is a link to the documentation for Integer.parseInt(String)
.
Upvotes: 3
Reputation: 48121
Just like the error says, the main method that will be invoked when you execute the class must have the signature public static void main(String[] args)
. You can't just give it different arguments.
If you pass numbers as arguments on the command line, they will be read in as strings by Java. If you want to convert them to a numeric type, you must do so explicitly in your code.
Upvotes: 2
Reputation: 77485
The signature of the main method cannot be changed. This is a constraint of the operating system.
Just parse the String into an integer via Integer.parseInt(...)
, then invoke the actual method!
Upvotes: 0