Avatar of shpark82
shpark82
 asked on

program to accept parameters from command line

i'm trying to figure out how this is done in java...


in command, i would like to type in:

java -classpath \documents\location MyClass 1 2 3

which would add the three numbers following the class name.

MyClass is the class name

\documents\location is the class path

1 2 3 would be the parameters



please help
Java

Avatar of undefined
Last Comment
CEHJ

8/22/2022 - Mon
CEHJ

You can get them through the args parameters of main
SOLUTION
CEHJ

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
shpark82

ASKER
how would i check if the args is int or string or float, etc?

for instance, if i put

1 12 123

as my parameters, would it recognize the numbers as

1,1,2,1,2,3?

or

1,12,123?

also what would happen if i put a letter such as 'A' in it for example

1 A 12

should i be checking if any of the args are not int and throw an exception?
CEHJ

The args are space-separated

>>should i be checking if any of the args are not int and throw an exception?

Yes, if that's a possibility
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
CEHJ

int sum = 0;
if(args.length > 0) {
      for(int i = 0;i < args.length;i++) {
            if (args[i].matches("\\d+")) {
                  sum += Integer.parseInt(args[i]);
            }
        
      }
}
System.out.println("Sum = " + sum);
Mick Barry

> 1,12,123?

it would recognise it as 3 strings

> should i be checking if any of the args are not int and throw an exception?

the code I posted above will throw an exception if its not a number

   int param1 = Integer.parseInt(args[0]);
CEHJ

This would work for floating point numbers too. If you're interested in int/long, cast to int/long:


double sum = 0;
if(args.length > 0) {
      for(int i = 0;i < args.length;i++) {
            try {
                  sum += Double.parseDouble(args[i]);
            }
            catch(NumberFormatException e) {
                  e.printStackTrace();
            }
        
      }
}
System.out.println("Sum = " + sum);
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.