command line arguments in java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mira88
    New Member
    • May 2007
    • 10

    command line arguments in java

    Hi, I'm new to java and would like to know how to take several command line arguments and sum them . I have done so for two numbers- but don't know how to go on. Thanks
    Code:
      
    public class TwoNumbers {
         public static void main(String args[]){
        	 
        	 int first_number = Integer.parseInt(args[0]);
        	 int second_number = Integer.parseInt(args[1]);
        	 int addition = first_number + second_number;
        	 
        	 if(args.length>2){    		 
        	 }
        	 System.out.println("The sum is :"+ addition);
         }
    }
  • blazedaces
    Contributor
    • May 2007
    • 284

    #2
    Originally posted by mira88
    Hi, I'm new to java and would like to know how to take several command line arguments and sum them . I have done so for two numbers- but don't know how to go on. Thanks
    Code:
      
    public class TwoNumbers {
         public static void main(String args[]){
        	 
        	 int first_number = Integer.parseInt(args[0]);
        	 int second_number = Integer.parseInt(args[1]);
        	 int addition = first_number + second_number;
        	 
        	 if(args.length>2){    		 
        	 }
        	 System.out.println("The sum is :"+ addition);
         }
    }
    Um... Thought your problem was that didn't work.

    Make a for loop with (int i = 0; i < args.length; i++) { /*declare total=0 earlier*/ total+=Integer. parse(args[i])

    Does that help?

    -blazed

    Comment

    • mira88
      New Member
      • May 2007
      • 10

      #3
      Originally posted by blazedaces
      What errors are you getting when trying to compile this?

      -blazed
      Im not gettin any errors ,but this programm does the addition for only two numbers and i want to know how to add for three or five or whatever numbers

      Comment

      • blazedaces
        Contributor
        • May 2007
        • 284

        #4
        Originally posted by mira88
        Im not gettin any errors ,but this programm does the addition for only two numbers and i want to know how to add for three or five or whatever numbers
        Note: message copied so user knows I posted.
        Um... Thought your problem was that didn't work. Didn't read completely, sorry.

        Make a for loop with (int i = 0; i < args.length; i++) { /*declare total=0 earlier*/ total+=Integer. parse(args[i])

        Does that help?

        -blazed

        Comment

        • mira88
          New Member
          • May 2007
          • 10

          #5
          yea it worked , thanks so much

          Comment

          • blazedaces
            Contributor
            • May 2007
            • 284

            #6
            Originally posted by mira88
            yea it worked , thanks so much
            I just want to know, for your sake, you understand why that worked right? I mean you even used args.length with your if-statement. So you're slightly familiar with arrays, right? If you want to iterate through them, use a for-loop (there are other ways).

            "As the great Andrew Martin once said, 'One is glad to be of service'."

            -blazed

            Comment

            • mira88
              New Member
              • May 2007
              • 10

              #7
              Well the way i understand it , as long as the length of the argument is greater than 0 initially, then the sum of the numbers will take place and then i will be increased by 1. Right?

              Comment

              • blazedaces
                Contributor
                • May 2007
                • 284

                #8
                Originally posted by mira88
                Well the way i understand it , as long as the length of the argument is greater than 0 initially, then the sum of the numbers will take place and then i will be increased by 1. Right?
                Yes, good. It's just, we're not supposed to just "give" you the answer, but rather guide you through it (my bad).

                Either way, good luck with java coding...

                -blazed

                Comment

                • mira88
                  New Member
                  • May 2007
                  • 10

                  #9
                  Thanks anyway, I have another question .I'm trying to do the same with multiplication and in my code I'm saying that if the argument length is greater than two then my multiplication should work - but its not. Can I initialise i with a 2 ,cause that is what I've done. I think I'm doing something wrong. Maybe you should guide me through :)
                  Code:
                    
                   public class MultiplyMoreNumbers {
                  	public static void main(String args[]){
                  		int multiply= 0;
                  		
                  		for(int i=2;i<args.length;i++){
                  			multiply = multiply*Integer.parseInt(args[i]);
                  		}
                  		System.out.println("The product is " + multiply);
                  	}
                  
                  }

                  Comment

                  • blazedaces
                    Contributor
                    • May 2007
                    • 284

                    #10
                    Originally posted by mira88
                    Thanks anyway, I have another question .I'm trying to do the same with multiplication and in my code I'm saying that if the argument length is greater than two then my multiplication should work - but its not. Can I initialise i with a 2 ,cause that is what I've done. I think I'm doing something wrong. Maybe you should guide me through :)
                    Code:
                      
                     public class MultiplyMoreNumbers {
                    	public static void main(String args[]){
                    		int multiply= 0;
                    		
                    		for(int i=2;i<args.length;i++){
                    			multiply = multiply*Integer.parseInt(args[i]);
                    		}
                    		System.out.println("The product is " + multiply);
                    	}
                    
                    }
                    Sorry, I laughed a little bit when I read it. I can completely understand the mistake.

                    You're actually making two mistakes.

                    This is what I suggest. If this happens again (if your output ends up 0 no matter what), write out the process on a piece of paper, just like I'll show you right now:

                    Code:
                    Ok, I want the args to be, let's say 1,2,3,4,5
                    Ok, so file starts.
                    
                    multiply = 0  Got it.
                    
                    for 2 to 4 times I will do this: multiply times integer of the corresponding arg {
                    2:  mutiply = multiply * arg[2] = 0 * 3 = 0;
                    3:  mutiply = multiply * arg[3] = 0 * 4 = 0;
                    4:  mutiply = multiply * arg[4] = 0 * 5 = 0;
                    }
                    
                    Ok, now print "The product is " + multiply which will print: The product is 0
                    What do you think you should change? One thing at a time.

                    Hope I'm guiding you through this well enough,

                    -blazed

                    Comment

                    • mira88
                      New Member
                      • May 2007
                      • 10

                      #11
                      Well, first I think i need to change my definition of multiply and secondly i have to redefine my for loop. Will do that and see if there is a change.

                      Comment

                      • mira88
                        New Member
                        • May 2007
                        • 10

                        #12
                        Hi I finally got it!! And it works , here is the corrected code .
                        Code:
                        
                        public class MultiplyMoreNumbers {
                            public static void main(String args[]){
                                int multiply= 1;
                                
                                for(int i=0;i<args.length;i++){
                                    multiply = multiply*Integer.parseInt(args[i]);
                                }
                                System.out.println("The product is " + multiply);
                            }
                         
                        }


                        Thanks soooo much

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #13
                          Originally posted by mira88
                          Hi I finally got it!! And it works , here is the corrected code .
                          Code:
                          
                          public class MultiplyMoreNumbers {
                              public static void main(String args[]){
                                  int multiply= 1;
                                  
                                  for(int i=0;i<args.length;i++){
                                      multiply = multiply*Integer.parseInt(args[i]);
                                  }
                                  System.out.println("The product is " + multiply);
                              }
                           
                          }


                          Thanks soooo much
                          That algorithm assumes that the multiplication of zero numbers equals one (1).

                          kind regards,

                          Jos ( <--- nitpicker ;-)

                          Comment

                          • mira88
                            New Member
                            • May 2007
                            • 10

                            #14
                            Oh I guess thats wrong then.

                            Comment

                            • JosAH
                              Recognized Expert MVP
                              • Mar 2007
                              • 11453

                              #15
                              Originally posted by mira88
                              Oh I guess thats wrong then.
                              Don't let me confuse you; the sum of zero numbers is assumed to be zero which
                              everyone finds acceptable; so why shouldn't the product of zero numbers be
                              equal to one then? Although not many people realize the assumption.

                              Your solution is fine as it is; don't let a nitpicking sloppy mathematician confuse you ;-)

                              kind regards,

                              Jos

                              Comment

                              Working...