JAVA ternary operator as a check for command line args


I'm coming from a javascript background, so it's possible that what I'm trying to do is not really a good way to do the same in JAVA.

I'd like to use some command line arguments to set up some values in my program. If the values are not supplied, I'd like to have default values.

this line works just fine:
int testIterations = Integer.parseInt(args[0]);

Open in new window

but I would like to do something like this, which is not compiling
int testIterations = Integer.parseInt(args[0]) != undefined ? Integer.parseInt(args[0]) : 50;

Open in new window

here's the error:
error: cannot find symbol
		int testIterations = Integer.parseInt(args[0]) != undefined ? Integer.parseInt(args[0]) : 50;
  symbol:   variable undefined
  location: class MyClass

Open in new window

if I use null instead of undefined, I get this error:
error: incomparable types: int and <null>
		int testIterations = Integer.parseInt(args[0]) != null ? Integer.parseInt(args[0]) : 50;

Open in new window

LVL 25
Kyle HamiltonData ScientistAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

why not just compare args[0] against null?  If null, no parameters.
        int testIterations = (args.length >= 1) ? Integer.parseInt(args[0])
                                                : DEFAULT_ITERATION_COUNT;

Open in new window

would do it

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kyle HamiltonData ScientistAuthor Commented:
thanks CEHJ.

Do you know if there is a good solution out there for creating flags?
(from my search online, JAVA doesn't appear to have this capability natively, like C)

so I could do something like this:

java MyClass -t50 -d100

any ideas? (let me know if you want me to open a new question)
Yes, a new question is necessary
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.