Java command passing args, help?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jimmy spearman
    New Member
    • Aug 2010
    • 5

    Java command passing args, help?

    Code:
    public class Echo {
        public static void main (String[] args) {
            for (String s: args) {
                System.out.println(s);
            if (args[2] == "help"){
    			System.out.println("help");
    			}
    		}
        }
    }
    im just simply testing to see if the first argument is equal to "help" your help is much appreciated.
  • Bret Little
    New Member
    • Aug 2010
    • 3

    #2
    Hi Jimmy,

    Because a String is an object, not a primitive type, you need to use the equals() method instead of ==

    In short, your code should be:
    Code:
    public class Echo {
        public static void main (String[] args) {
            for (String s: args) {
                System.out.println(s);
            if (args[2].equals("help")) {
                System.out.println("help");
                }
            }
        }
    }

    Comment

    • jimmy spearman
      New Member
      • Aug 2010
      • 5

      #3
      Originally posted by Bret Little
      Hi Jimmy,

      Because a String is an object, not a primitive type, you need to use the equals() method instead of ==

      In short, your if state4ment should be:
      Code:
      public class Echo {
          public static void main (String[] args) {
              for (String s: args) {
                  System.out.println(s);
              if (args[2].equals("help")) {
                  System.out.println("help");
                  }
              }
          }
      }
      thank you for the help and it means a lot because im trying to better myself in java hoping to get some kind of job in the field of but im only 16 so there are some year to go.

      Comment

      • jimmy spearman
        New Member
        • Aug 2010
        • 5

        #4
        now i get an error
        and got rid of the for statement
        Code:
        Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
                at Echo.main(Echo.java:5)

        Comment

        • Bret Little
          New Member
          • Aug 2010
          • 3

          #5
          Jimmy,

          The point of your for loop is to check each element of args to see if it is equal to "help". Args is an array of Strings that you pass into your program when you run it. It could have a length of 2,3, or 20. Because of this, you need to test if s.equals("help" ) rather than args[2].equals("help") . If you just test for args[2], then you are only checking for the 3rd element of the array, and in the case of the error you posted, it does not have three elements and therefore throws an exception.

          Comment

          • jimmy spearman
            New Member
            • Aug 2010
            • 5

            #6
            Originally posted by Bret Little
            Jimmy,

            The point of your for loop is to check each element of args to see if it is equal to "help". Args is an array of Strings that you pass into your program when you run it. It could have a length of 2,3, or 20. Because of this, you need to test if s.equals("help" ) rather than args[2].equals("help") . If you just test for args[2], then you are only checking for the 3rd element of the array, and in the case of the error you posted, it does not have three elements and therefore throws an exception.
            ok so i figured it but, for the long term. how do i suppress
            a thrown exception in the console

            Comment

            • Oralloy
              Recognized Expert Contributor
              • Jun 2010
              • 988

              #7
              Use a try/catch/finally block to catch exceptions.

              Still, exceptions exist for a reason. Just suppressing them is usually a bad idea. Instead, your code should behave in a reasonable way.

              If you think about it, the most reasonable behaviour is to tell you, the user/developer, exactly what happened, which is what your exception in post #5 does.

              Comment

              • jimmy spearman
                New Member
                • Aug 2010
                • 5

                #8
                Thank you for all who helped me on this post but i gave up on java and started learning python.

                Comment

                Working...