Results 1 to 9 of 9
  1. #1
    denisatandi is offline Member
    Join Date
    Feb 2008
    Posts
    1
    Rep Power
    0

    Default Command line argument

    I'm new to Java, can someone please tell me how you can use a command line argument to print a message several times? Say if you wanted to print Hello world three times and you have to enter 3 for the message to print three times.
    Thank you

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    In Java command-line arguments are passes to the application main method using an array of strings.

    Java Code:
    public static void main (String[] arguments)
    {
    // Body of the main method.
    }
    If you want to use a numeric value, you have to get it from a string array. Something like this,

    Java Code:
    int numericValue = Integer.parseInt(arguments[0]);
    this line of code gives a numeric value. So in your application you can do this.

    Java Code:
    class Test
    {
    public static void main(String[] arguments)
    {
    int numberOfTurns = Integer.parseInt(arguments[0]);
    while(0 < numberOfTurns)
    {
    System.out.println("Hello World");
    numberOfTurns--;
    }
    }
    }

  3. #3
    ferranb is offline Member
    Join Date
    Jun 2008
    Posts
    22
    Rep Power
    0

    Default

    Notice that in java the argument 0 is not the executable like others languages like C/C++.

    Ferran

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    I'm not clear what you are talking about. In my explanation, I'm not talking about anything related to executable commands. Basically over friend felt difficult with how to handle command line arguments in Java. May be the following code snippets can be helped. But I don't want to send this earlier because there is no response from thread starter. :)

    Java Code:
    class Test {
    	public static void main(String[] arguments) {
    		else if(arguments.length == 2){
    			String text = arguments[0];
    			int terms = Integer.parseInt(arguments[1]);
    			for(int i = 0; i < terms; i++) {
    				System.out.println(text);
    			}
    		}
    		else {
    			System.out.println("Invalid");
    		}
    	}
    }
    Here you need two command parameters to run the code.

  5. #5
    niloufar is offline Member
    Join Date
    Sep 2012
    Posts
    26
    Rep Power
    0

    Question Re: Command line argument

    Hi, I'm using Comment arguments and can't pass those values into my validatation method and not sure were to parse my string args to int. can you check my code and let me know what i'm doing wrong Pleaaaaaaaaase? Thank you

    Java Code:
    public class test {
    	
    	// Global Constants
        final static int MIN_NUMBER = 1;
        final static int MAX_PRIME = 10000;
        final static int MAX_FACTORIAL = 12;
        final static int MAX_LEAPYEAR = 4000;
      
        //Global Variable
        static int a,b,c;
    	
    	
    public static void main (String[] args) {
    		String[] myNumbers= new String [3];
    				
    		for(int i =0; i< args.length; i++){
    			//System.out.print(args[i]+" ");
    			validateInput(myNumbers);
    			
    		
    		}
    		
    	}
    			
    					
    //Validate User Input
    	public static boolean validateInput(String[] array){
    		String[] arrayCopy = copyArray(array);
    		boolean isValid = false;
    				
    		try {
    			
    			for(int i = 0; i < array.length; i++) {
    			a=Integer.parseInt(arrayCopy[0]);
    			if(!withinRange(array)) {
                  isValid = true; 
    	          System.out.println("The entered value " + array[0] + " is not a valid integer.  Please try again.");
    			}
    		} 
    	}
    			 catch(Exception ex) { 
    	          
    	        }
    	        return isValid;
    	    }
    	
    	
    	/**
    	 * Copies the array.
    	 * @param array The array to copy.
    	 * @return A copy of the input array.
    	 */	
    	private static String[] copyArray(String[] array) {
    		String[] copy = new String[array.length]; // Declare an array with a size equal to the length of the input array
    		
    		for(int x = 0; x < copy.length; x++) {
    			copy[x] = array[x]; // Assign each value of the input array to the new array
    		}
    		return copy; // Return the copied array
    	}
    	
    //Check the value within the specified range
    		private static boolean withinRange(String [] array) {
    			String[] arrayCopy= copyArray(array);
    			
    	     boolean isInRange = true; 
    	    
    	     for(int i =0; i< arrayCopy.length; i++){
    	     if(arrayCopy.length < MIN_NUMBER && arrayCopy.length > MAX_PRIME) {
    	         System.out.println("The entered value "+ (arrayCopy[i]) +" is out of range [1 TO 1000].");
    	         isInRange = false; 
    	     }
    	    
    	     }
    	     return isInRange;
    	 }
    		}
    		
    		
    }

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default Re: Command line argument

    Do not ressurect old threads. Especially when you already have asked this question in another thread.

  7. #7
    niloufar is offline Member
    Join Date
    Sep 2012
    Posts
    26
    Rep Power
    0

    Default Re: Command line argument

    but nobody has answered me yet, i'm looking for some help! sorry :(

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default Re: Command line argument

    Oh boo hoo!

  9. #9
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Command line argument

    Please don't resurrect posts that are over 4 years old, especially to hijack them. Thread locked.

Similar Threads

  1. Java command line
    By agouzoul in forum New To Java
    Replies: 2
    Last Post: 04-02-2008, 12:12 PM
  2. calling linux command line in java
    By fangzhong in forum New To Java
    Replies: 0
    Last Post: 02-03-2008, 04:24 PM
  3. Printing command line arguments
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-03-2007, 09:27 AM
  4. Exporting from the command line
    By o1121 in forum Eclipse
    Replies: 1
    Last Post: 08-09-2007, 07:29 PM
  5. Unable to execute command line command in java
    By LordSM in forum New To Java
    Replies: 1
    Last Post: 08-08-2007, 12:23 AM

Posting Permissions

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