DescX
DescX

Reputation: 334

Changing main parameter type

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

Answers (11)

Simon Woker
Simon Woker

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

eggonlegs
eggonlegs

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

Jean-Charles
Jean-Charles

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

Lobo
Lobo

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

Balaswamy Vaddeman
Balaswamy Vaddeman

Reputation: 8540

write a main method like below

public static void main(String[] args){

}.

and use Integer.parseInt(str)

Upvotes: 1

carpamon
carpamon

Reputation: 6633

Try sending your integer in the String array.

new String[]{"1"}

Then fetch it:

Integer yourInteger = Integer.valueOf(args[0])

Upvotes: 2

Tudor
Tudor

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

Hecksa
Hecksa

Reputation: 2842

Just pass in a string, and then use Integer.parseInt(string) to get the integer you need back.

Upvotes: 1

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

Dave Costa
Dave Costa

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

Has QUIT--Anony-Mousse
Has QUIT--Anony-Mousse

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

Related Questions