Results 1 to 14 of 14

Thread: help with java

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Posts
    122

    help with java

    sorry for lack of a descriptive title, but I don't know how to describe my problem!

    I'm looking to do this:

    I have a program called "program" It is run via the command "java" in the command line. I have 3 strings in it: string "a", string "b", and string "c". Here's what I need to have happen:

    When the user enters "java program duck cow moose". I want "duck" to be stored in string "a", "cow" to be stored in string "b", and "moose" to be stored in string "c". Also, If the person only enters "java program duck cow", then I want "duck" to be stored in string "a", "cow" to be stored in string "b", and for string "c" to remain empty.

    I have no idea how to go about this... I know how to make it so user input goes into a string, but only after the program is running. How do I do it in the way I described?

    Thanks,
    stettybet0

  2. #2
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: help with java

    Code:
    public static void main(String[] args)
    {
    }
    args will contain the passed arguments. args[0] = first argument, args[1] = second argument, etc.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Posts
    122

    Re: help with java

    just to make sure I understand:

    so, if i put
    Code:
    string a = args[0]
    string b = args[1]
    string c = args[2]
    that would give me what I want?

  4. #4
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: help with java

    Quote Originally Posted by stettybet0
    just to make sure I understand:

    so, if i put
    Code:
    string a = args[0]
    string b = args[1]
    string c = args[2]
    that would give me what I want?
    Sort of. Your code would require exactly 3 arguments, and since you might not have three you need to do some range checking.

    ie: if (args.length > 0)

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Posts
    122

    Re: help with java

    to check whether args[0] exists, would i do "if (args[0].length > 0)" ?

  6. #6
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: help with java

    if (args.length > 0) ensures that there's at least one argument, so args[0] would exist in this case. It might be helpful to know that args.length returns the number of arguments. If you know there won't be more than 3 arguments, then you can do something like this:

    Code:
       public static void main(String[] args)
       {
            String a="", b="", c="";
    	if (args.length > 0) a = args[0];
    	if (args.length > 1) b = args[1];
    	if (args.length > 2) c = args[2];
       }
    or

    Code:
       public static void main(String[] args)
       {
            String a="", b="", c="";
    	switch(args.length)
    	{
    		case 1:
    			a = args[0];
    			break;
    		case 2: 
    			a = args[0];
    			b = args[1];
    			break;
    		case 3:
    			a = args[0];
    			b = args[1];
    			c = args[2];
    			break;
    	}
       }

    This could be done in many different ways.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Posts
    122

    Re: help with java

    here's what I tried... with no success:

    java Code:
    1. class program {
    2.  
    3.    public static void main (String[] args) {
    4.  
    5.       String a = null;
    6.       String b = null;
    7.       String c = null;
    8.  
    9.       if (args[0].length > 0) {
    10.          String a = args[0];
    11.       }
    12.       if (args[1].length > 0) {
    13.          String b = args[1];
    14.       }
    15.       if (args[2].length > 0) {
    16.          String c = args[2];
    17.       }
    18.       System.out.println(a);
    19.       System.out.println(b);
    20.       System.out.println(c);
    21.    }
    22. }

    edit:
    saw ur new post... trying that out now.

  8. #8
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: help with java

    Wow! We just posted at the exact same time! You are very close, but check my listings about you.

    I would go for this:

    Code:
       public static void main(String[] args)
       {
            String a="", b="", c="";
    	if (args.length > 0) a = args[0];
    	if (args.length > 1) b = args[1];
    	if (args.length > 2) c = args[2];
       }

  9. #9
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: help with java

    Of course, if you just want to print out the args, then all you have to do is this:

    Code:
       public static void main(String[] args)
       {
    	for (int i=0; i<args.length; ++i) 
    		System.out.println(args[i]);
       }

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Posts
    122

    Re: help with java

    Here's what I ended up with: (it works great)

    java Code:
    1. class program {
    2.  
    3.    public static void main (String[] args) {
    4.  
    5.       String a = "";
    6.       String b = "";
    7.       String c = "";
    8.  
    9.       if (args.length > 0 && args.length < 2) {
    10.          a = args[0];
    11.          System.out.println(a);
    12.       }
    13.       if (args.length > 1 && args.length < 3) {
    14.          a = args[0];
    15.          b = args[1];
    16.          System.out.println(a);
    17.          System.out.println(b);
    18.       }
    19.       if (args.length > 2 && args.length < 4) {
    20.          a = args[0];
    21.          b = args[1];
    22.          c = args[2];
    23.          System.out.println(a);
    24.          System.out.println(b);
    25.          System.out.println(c);
    26.       }
    27.       if (args.length > 3 || args.length < 1) {
    28.          System.out.println("Enter only 1, 2, or 3 arguments!");
    29.       }
    30.    }
    31. }

    anything you see that i did wrong (or inefficiently)?

    thanks for the help.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Posts
    122

    Re: help with java

    I have also come up with this way:

    java Code:
    1. class program {
    2.  
    3.    public static void main (String[] args) {
    4.  
    5.       String a = "";
    6.       String b = "";
    7.       String c = "";
    8.  
    9.       if (args.length > 3 || args.length < 1) {
    10.          System.out.println("Enter only 1, 2, or 3 arguments!");
    11.          System.exit(0);
    12.       }
    13.       if (args.length > 0) {
    14.          a = args[0];
    15.          System.out.println(a);
    16.       }
    17.       if (args.length > 1) {
    18.          b = args[1];
    19.          System.out.println(b);
    20.       }
    21.       if (args.length > 2) {
    22.          c = args[2];
    23.          System.out.println(c);
    24.       }
    25.    }
    26. }

    Which way do you think is more "orthodox"? (ie. it would please my old-skool teacher)

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Posts
    122

    Re: help with java

    A third way: (only difference is that I don't define the variables before I need them... just learned that you usually shouldn't )

    java Code:
    1. class program {
    2.  
    3.    public static void main (String[] args) {
    4.  
    5.       if (args.length > 3 || args.length < 1) {
    6.          System.out.println("Enter only 1, 2, or 3 arguments!");
    7.          System.exit(0);
    8.       }
    9.       if (args.length > 0) {
    10.          String a = args[0];
    11.          System.out.println(a);
    12.       }
    13.       if (args.length > 1) {
    14.          String b = args[1];
    15.          System.out.println(b);
    16.       }
    17.       if (args.length > 2) {
    18.          String c = args[2];
    19.          System.out.println(c);
    20.       }
    21.    }
    22. }

    This way seems the best to me. My teacher always says that if you can do the same thing with less code, then do it!

  13. #13
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: help with java

    If you are just printing the variables out, then there's no point in even creating them:

    Code:
    public static void main(String[] args)
    {
       if (args.length > 0 && args.length < 4)
       {
           for (int i=0; i<args.length; ++i)
             System.out.println(args[i]);
       }
       else
       {
           System.out.println("You must supply at least one argument and less than four");
       }
    }
    No variables needed, but of course, if this is an assignment and it's required then go with your last try.

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Posts
    122

    Re: help with java

    Yeah, I needed variables. Thanks for your help!

Posting Permissions

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



Click Here to Expand Forum to Full Width