Hi,
How do i take a command line input and place it in an array?:confused:
This has been driving me nuts for two days.
please please please help
Printable View
Hi,
How do i take a command line input and place it in an array?:confused:
This has been driving me nuts for two days.
please please please help
You use the args array that main has to access command line input, each new word on the command line represents a new string, you can group words on cmd with quotes.
args[0] is aCode:java MyProg a b c "a b c"
args[1] is b
args[2] is c
args[3] is "a b c"
From there you treat them as normal data and store them in an array the way you would store a few strings in an array.
Thanks heaps for your help! just so im clear
in your example
a b c "abc"
if i typed oranges without the""
would that go
o or oranges
r
a
n
g
e
s
Ifeach letter would go individual how could i get the program to add whole words without ""
also how then would could i list the contents of my arg array?
I suppose I should have used more letters in the words, it basically splits the command input based on spaces, if the only argument was oranges, args[0] would contain oranges. You use quotes when you want more than one word in an element you use quotes. You list them the same way you would list any array.
args[0] is orangesCode:java Program oranges apples "apples and oranges"
args[1] is apples
args[2] is apples and oranges
Thats great thanks heaps!! :D
any hints on displaying args[]
How would you display any other array passed to a method as an argument? Itsthe same for the args array in main.
Im sure one day i will know the answer to that however day 3 isn't one of them :)
The args[] makes solid sense to me now thanks very much
You use a loop to loop through the array and access the elements. The args array main takes is populated with the command line arguments
This simple article would help you with that : Command Line Arguments
Hope that helps,
Goldest
thanks for the great link!
So could you please help me understand this?
*I create a class called echo and put the echo class in it;
*then i some how reference the echo class from within my main class???
*then when main class is run ;
*calls on the echo class to do its thing
Is that the jist of it? if so is this "structure" kind of typical for java programs?
Or am i hopelessly lost and should be put down? ;)
Dont get main confused for a class, it's a static that acts as the start of your class.
The main method takes an array of strings that will be filled with the command line arguments. In the example here, echo is not a "real" class in oo terms, it's more of just a way to name the program. All java programs must be in the form of a class.
The main method uses a for each loop, a for each loop means for each type in list x.
it extracts each string in the args array and temporarily assigns it to s. Then you use the s object to do stuff. Here is the same thing with a regular for loopCode:for(String s : args)
I hope this has helped to clarify what the program does. Ask more questions if you still aren't entirely clear.Code:for(int i = 0; i < args.length; ++i){
String s = args[i];
System.out.println(s);
}
You are welcome, glad to have helped. Please mark your thread solved with the thread tools at the top of the page.