Java Programming

Discussion in 'Mac Programming' started by crtvmac, Mar 16, 2010.

  1. crtvmac Guest

    Joined:
    Aug 14, 2009
    #1
    Will someone tell me the error in my programming please? The output I get when I run the program below is: args.length = 0. I also get ArrayIndexOutOfBoundsException in argument two.


    Code:
    public class CommandLine {
      public static void main(String args[ ]) {
         System.out.println("args.length = " + args.length);
         System.out.println("args[0] = " + args[0]);
         System.out.println("args[1] = " + args[1]);
         System.out.println("args[2] = " + args[2]);
        }
    }

    Shouldn't the output be ?
    args.length = 3
    arg.[0] = 1
    arg.[1] = 2
    arg.[2] = 3
     
  2. iShater macrumors 604

    iShater

    Joined:
    Aug 13, 2002
    Location:
    Chicagoland
    #2
    Please use the
    Code:
     tag to mark your code for easier readability.
    
    Do you understand what your code is supposed to do?
     
  3. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #3
    How many arguments did you pass to the program when you ran it? Did you pass "1 2 3"? If not, what did you pass?

    -Lee
     
  4. iShater macrumors 604

    iShater

    Joined:
    Aug 13, 2002
    Location:
    Chicagoland
    #4
    How did you run your program should be our first question.
     
  5. sanPietro98 macrumors 6502a

    sanPietro98

    Joined:
    May 30, 2008
    Location:
    28.416834,-81.581214
    #5
    The elements in the 'args' array depends on what you type into the command line when you try to run the program. For instance:

    Code:
    java CommandLine
    will give you an args array of length 0.

    Code:
    java CommandLine alpha bravo charlie
    will give you an args array length of 3.
     
  6. crtvmac thread starter Guest

    Joined:
    Aug 14, 2009
    #6
     
  7. crtvmac thread starter Guest

    Joined:
    Aug 14, 2009
    #7
    I show you the program. Thats is as far as I understand the program. Thats is way I displayed the program and ask for help. Any other information about this program, I don't understand.
     
  8. crtvmac thread starter Guest

    Joined:
    Aug 14, 2009
    #8
    Java Programming

    I ran the program using BlueJ complier. Is there a better way?
     
  9. sanPietro98 macrumors 6502a

    sanPietro98

    Joined:
    May 30, 2008
    Location:
    28.416834,-81.581214
    #9
    We see your program. We need to see how you executed the program to diagnose the issue.
     
  10. sanPietro98 macrumors 6502a

    sanPietro98

    Joined:
    May 30, 2008
    Location:
    28.416834,-81.581214
    #10
    Some IDE (Integrated Development Environments) don't make it easy to pass arguments into your "main" method.

    Do you know how to run it from the command line?

    If you're using a Mac, open the Terminal app, find your .class files and type:

    Code:
    java CommandLine arg1 arg2 arg3
    the three tokens after CommandLine will be what is populated into your 'args' array.
     
  11. sanPietro98 macrumors 6502a

    sanPietro98

    Joined:
    May 30, 2008
    Location:
    28.416834,-81.581214
    #11
  12. crtvmac thread starter Guest

    Joined:
    Aug 14, 2009
    #12
    All I know about the program is what you see. Blink
     
  13. crtvmac thread starter Guest

    Joined:
    Aug 14, 2009
    #13
    Java Programming

    "Thank you all for assisting me, by answering my question with a question."
     
  14. iShater macrumors 604

    iShater

    Joined:
    Aug 13, 2002
    Location:
    Chicagoland
    #14
     
  15. iShater macrumors 604

    iShater

    Joined:
    Aug 13, 2002
    Location:
    Chicagoland
    #15
    The questions are actually all legit, because most of the info is needed to understand what you might be doing wrong.

    They are also trying to help you understand the program. Teaching you how to fish kinda thing. :eek:
     
  16. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #16
    The BlueJ website tells you how to pass command-line args to your main class:

    http://www.bluej.org/help/archive.html#tip9

    I recommend copying and pasting the text from the web-page. It appears to be custom-made for your CommandLine class.


    The above link came from BlueJ FAQs:
    http://www.bluej.org/help/faq.html#main-args

    Which I found by typing bluej command line into google.


    The simple answer to what's wrong with your program is it blindly assumes the array it receives has 3 elements. This may not always be true, since the array passed to main() represents command-line parameters entered on the command-line.

    If any method receives an array parameter and doesn't already know the length of the array, it should be written to use the actual length of the received array. This is because the receiver (i.e. the method that was called) doesn't get to choose the length. The sender (i.e. the point where the target method was called) is the one who chooses what the receiver receives.

    If you entered that program from a book or tutorial, it should have told you how to run it. Since you haven't said what book or tutorial you're working from, if any, no one can offer you better advice. You have to tell us what you're working from. The program didn't write itself.
     
  17. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #17
    crtvmac: You are assuming we're trying to give you a hard time, etc. What we're really trying to do is shed light on why you got the behavior you got. Essentially, this program:
    prints "args.length = ", then displays the number of command line arguments passed to the program. if none are passed, this will display 0.

    next, the program prints "args[0] = ", then assumes there is at least one arguments, and prints the first argument.

    next, the program prints "args[1] = ", then assumes there are at least two arguments, and prints the second argument.

    next, the program prints "args[2] = ", then assumes there are at least three arguments, and prints the third argument.

    If you don't pass any arguments, then the assumptions for everything but the first print are incorrect, so you'll get an error (in this case, an exception, a special sort of error) like the one you experienced.

    This (found by googling "BlueJ arguments") explains how to set the arguments to main (normally passed on the command line):
    http://www.javakb.com/Uwe/Forum.aspx/java-programmer/140/BlueJ-Arguments

    -Lee

    Edit: Also, one should never assume any non-primitive (array,object, etc.) is non-null. I don't know if there are platforms where args passed to main are null instead of length 0, but in general, checking things for null before using .length is best.
     
  18. sanPietro98 macrumors 6502a

    sanPietro98

    Joined:
    May 30, 2008
    Location:
    28.416834,-81.581214
    #18
    Also, you should practice "defensive coding." Never assume an array has a particular length, then blindly try to access an element at a particular index. In other words, before accessing the 2nd index, make sure it's really there.

    Instead of this...
    Code:
    
    System.out.println(args[2]);
    
    
    You should do this...
    Code:
    
    if (args.length >= 3)
      System.out.println(args[2]);
    
    
    Just a friendly tip from someone who's been coding for 20 years. (Whoa that's a depressing thought).
     
  19. pikester macrumors newbie

    pikester

    Joined:
    Feb 27, 2009
    #19
    The code works (bold text is stuff I typed):
    Code:
    [B]vi CommandLine.java
    javac CommandLine.java 
    java -cp . CommandLine 1 2 3[/B]
    args.length = 3
    args[0] = 1
    args[1] = 2
    args[2] = 3
     

Share This Page