Results 1 to 9 of 9
Thread: Command line argument
- 02-18-2008, 12:18 PM #1
Member
- Join Date
- Feb 2008
- Posts
- 1
- Rep Power
- 0
- 03-03-2008, 06:39 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
In Java command-line arguments are passes to the application main method using an array of strings.
If you want to use a numeric value, you have to get it from a string array. Something like this,Java Code:public static void main (String[] arguments) { // Body of the main method. }
this line of code gives a numeric value. So in your application you can do this.Java Code:int numericValue = Integer.parseInt(arguments[0]);
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--; } } }
- 06-14-2008, 03:23 AM #3
Member
- Join Date
- Jun 2008
- Posts
- 22
- Rep Power
- 0
Notice that in java the argument 0 is not the executable like others languages like C/C++.
Ferran
- 06-15-2008, 06:59 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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. :)
Here you need two command parameters to run the code.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"); } } }
- 10-16-2012, 05:39 AM #5
Member
- Join Date
- Sep 2012
- Posts
- 26
- Rep Power
- 0
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; } } }
- 10-16-2012, 06:38 AM #6
Re: Command line argument
Do not ressurect old threads. Especially when you already have asked this question in another thread.
- 10-16-2012, 06:41 AM #7
Member
- Join Date
- Sep 2012
- Posts
- 26
- Rep Power
- 0
Re: Command line argument
but nobody has answered me yet, i'm looking for some help! sorry :(
- 10-16-2012, 06:46 AM #8
Re: Command line argument
Oh boo hoo!
- 10-16-2012, 11:37 PM #9
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,619
- Rep Power
- 5
Similar Threads
-
Java command line
By agouzoul in forum New To JavaReplies: 2Last Post: 04-02-2008, 12:12 PM -
calling linux command line in java
By fangzhong in forum New To JavaReplies: 0Last Post: 02-03-2008, 04:24 PM -
Printing command line arguments
By Java Tip in forum Java TipReplies: 0Last Post: 12-03-2007, 09:27 AM -
Exporting from the command line
By o1121 in forum EclipseReplies: 1Last Post: 08-09-2007, 07:29 PM -
Unable to execute command line command in java
By LordSM in forum New To JavaReplies: 1Last Post: 08-08-2007, 12:23 AM


LinkBack URL
About LinkBacks

Bookmarks