0

Hello everyone,

How do I use command line arguments from main(String[] args) as user input in java?

My problem is that I need to make a program for an assignment. It needs to execute in 2 ways.
If no arguments are entered,
Ie. java myprogram
it opens a menu that allows the user to select an option and asks the user to input the parameters for that option.

If arguments are entered,
Ie. java my program A 5 H
It uses 'A' as the option, and '5' and 'H' as the required parameters.

I understand that:
args[0] = A
args[1] = 5
args[2] = H

I'm just unsure how to pass this information to my other methods in addition to having the option for user input via the menu.
Essentially, I can get it to do one or the other, not both. I'm pretty new to java and this is my first assignment. Everything else is working fine, I just can't get this going. I'll post code if need be but since this is a university assignment, I don't really want to get caught "cheating" considering students and teachers probably use/read this forum.

Thanks in advance!

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by NormR1
0

how to pass this information to my other methods in addition to having the option for user input via the menu.

Use if/else logic. In pseudo code:
If there are args, load the values for the method calls from those args
else If there are no args, ask the user for the values to use
When you get here, the values have been set, either from the args or from the user.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.