Results 1 to 14 of 14
  1. #1
    minusten is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Question command line input to array

    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

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

    Java Code:
    java MyProg a b c "a b c"
    args[0] is a
    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.

  3. #3
    minusten is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default

    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 ""

  4. #4
    minusten is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default

    also how then would could i list the contents of my arg array?

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

    Java Code:
    java Program oranges apples "apples and oranges"
    args[0] is oranges
    args[1] is apples
    args[2] is apples and oranges
    Last edited by sunde887; 04-27-2011 at 05:55 AM.

  6. #6
    minusten is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default

    Thats great thanks heaps!! :D

    any hints on displaying args[]

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    How would you display any other array passed to a method as an argument? Itsthe same for the args array in main.

  8. #8
    minusten is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default

    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

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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

  10. #10
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    Quote Originally Posted by minusten View Post
    any hints on displaying args[]
    This simple article would help you with that : Command Line Arguments

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  11. #11
    minusten is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default

    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? ;)

  12. #12
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.
    Java Code:
    for(String s : args)
    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 loop
    Java Code:
    for(int i = 0; i < args.length; ++i){
      String s = args[i];
      System.out.println(s);
    }
    I hope this has helped to clarify what the program does. Ask more questions if you still aren't entirely clear.

  13. #13
    minusten is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    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.
    Java Code:
    for(String s : args)
    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 loop
    Java Code:
    for(int i = 0; i < args.length; ++i){
      String s = args[i];
      System.out.println(s);
    }
    I hope this has helped to clarify what the program does. Ask more questions if you still aren't entirely clear.
    Very Helpful thank you :)

  14. #14
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You are welcome, glad to have helped. Please mark your thread solved with the thread tools at the top of the page.

Similar Threads

  1. can i run line by line command in netbean?
    By choconlongxu in forum NetBeans
    Replies: 1
    Last Post: 07-19-2010, 09:41 PM
  2. Replies: 2
    Last Post: 07-02-2010, 03:20 PM
  3. Replies: 3
    Last Post: 04-26-2010, 10:00 PM
  4. Replies: 22
    Last Post: 01-05-2009, 02:03 AM
  5. Unable to execute command line command in java
    By LordSM in forum New To Java
    Replies: 1
    Last Post: 08-08-2007, 01:23 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •