Results 1 to 18 of 18
- 10-15-2012, 09:07 AM #1
Member
- Join Date
- Sep 2012
- Posts
- 26
- Rep Power
- 0
command-line arguments
Hi, I know my codes are mess, but i'm new and desperately need help. you guys are my last hope!
here is my code and I have 3 int that only one of them passes by method. wil check my code and tell me why? thanx
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 userInput, a, b, c; public static void main (String[] args) { if(args.length !=3){ System.err.println("Please enter 3 integers seperated with spaces."); System.exit(0); } for(int i =0; i< args.length; i++){ validateInput(args[0], args[1], args[2]); //performOperations(userInput); //displayReport(); } } //Validate User Input private static boolean validateInput(String value1, String value2, String value3){ boolean isValid = false; try { a= Integer.parseInt(value1); if(!withinRange(a, MIN_NUMBER, MAX_PRIME)) { isValid = true; System.out.println("The entered value " + value1 + " is not a valid integer. Please try again."); } b= Integer.parseInt(value2); if(!withinRange(b, MIN_NUMBER, MAX_FACTORIAL)) { isValid = true; System.out.println("The entered value " + value2 + " is not a valid integer. Please try again."); } c= Integer.parseInt(value3); if(!withinRange(c, MIN_NUMBER, MAX_LEAPYEAR)) { isValid = true; System.out.println("The entered value " + value3 + " is not a valid integer. Please try again."); } } catch(Exception ex) { } return isValid; } //Check the value within the specified range private static boolean withinRange(int value, int minNumber, int maxNumber) { boolean isInRange = true; if(a < MIN_NUMBER && a >MAX_PRIME) { System.out.println("The entered value is out of range [1 TO 1000]."); isInRange = false; } if(b < MIN_NUMBER || b > MAX_FACTORIAL) { System.out.println("The entered value is out of range [1 TO 12]."); isInRange = false; } if(c < MIN_NUMBER || c > MAX_LEAPYEAR) { System.out.println("The entered value is out of range [1 TO 4000]."); isInRange = false; } return isInRange; } }
Last edited by niloufar; 10-16-2012 at 12:12 AM.
- 10-15-2012, 01:35 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 26
Re: my command-line argument doesn't pass values!
What do you see when you run your code?
What do you expect to see?
I would recommend printing out all the values you receive from the command line in your main method to see what it's getting.
Also, how are you running this?
That is, what is the full command you are using?Please do not ask for code as refusal often offends.
** This space for rent **
- 10-15-2012, 06:15 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 26
Re: my command-line argument doesn't pass values!
Java Code:if(args.length !=3){ System.err.println("Please enter 3 integers seperated with spaces."); System.exit(0); }
Print them out before you do anything else.
As I suggested you do.Please do not ask for code as refusal often offends.
** This space for rent **
- 10-15-2012, 07:29 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 26
Re: my command-line argument doesn't pass values!
So show us what values are output when you print them to the screen.
Also get your code to print out the values of a, b and c after they have been parsed from the strings.Please do not ask for code as refusal often offends.
** This space for rent **
- 10-16-2012, 12:09 AM #5
Member
- Join Date
- Sep 2012
- Posts
- 26
- Rep Power
- 0
Re: my command-line argument doesn't pass values!
up to here, everything works:
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 userInput,a,b,c; public static void main (String[] args) { for(int i =0; i< args.length; i++){ userInput= Integer.parseInt(args[i]); System.out.print(userInput+" "); }
and result is :1500 11 1900
however, i need to assign each value to another method calls validatedInput();
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 userInput,a,b,c; public static void main (String[] args) { for(int i =0; i< args.length; i++){ userInput= Integer.parseInt(args[i]); validateInput(args[0],args[1],args[2]); //System.out.print(userInput+" "); } } //Validate User Input public static boolean validateInput(String num1, String num2, String num3){ boolean isValid = false; try { a= Integer.parseInt(num1); if(!withinRange(a, MIN_NUMBER, MAX_PRIME)) { isValid = true; System.out.println("The entered value " + num1 + " is not a valid integer. Please try again."); } int b= Integer.parseInt(num2); if(!withinRange(b, MIN_NUMBER, MAX_FACTORIAL)) { isValid = true; System.out.println("The entered value " + num2 + " is not a valid integer. Please try again."); } int c= Integer.parseInt(num3); if(!withinRange(c, MIN_NUMBER, MAX_LEAPYEAR)) { isValid = true; System.out.println("The entered value " + num3 + " is not a valid integer. Please try again."); } } catch(Exception ex) { } return isValid; } //Check the value within the specified range private static boolean withinRange(int value, int minNumber, int maxNumber) { boolean isInRange = true; if(a < MIN_NUMBER && a >MAX_PRIME) { System.out.println("The entered value "+ a +" is out of range [1 TO 1000]."); isInRange = false; } if(b < MIN_NUMBER || b > MAX_FACTORIAL) { System.out.println("The entered value "+b+" is out of range [1 TO 12]."); isInRange = false; } if(c < MIN_NUMBER || c > MAX_LEAPYEAR) { System.out.println("The entered value "+c+" is out of range [1 TO 4000]."); isInRange = false; } return isInRange; } }
The entered value 0 is out of range [1 TO 12].
The entered value 0 is out of range [1 TO 4000].
The entered value 1500 is not a valid integer. Please try again.
The entered value 0 is out of range [1 TO 12].
The entered value 0 is out of range [1 TO 4000].
The entered value 11 is not a valid integer. Please try again.
The entered value 0 is out of range [1 TO 12].
The entered value 0 is out of range [1 TO 4000].
The entered value 1900 is not a valid integer. Please try again.
- 10-16-2012, 04:41 AM #6
Re: my command-line argument doesn't pass values!
In your validateInput method you are creating local variables b and c which are different to class variables.
- 10-16-2012, 05:35 AM #7
Re: my command-line argument doesn't pass values!
Huh?
Look at your validateInput method and see what the difference between variable a (which is working) and variables b and c (which are not working).
- 10-16-2012, 06:12 AM #8
Member
- Join Date
- Sep 2012
- Posts
- 26
- Rep Power
- 0
New Error
Hi, Thanks for helping me. I changed some stuff and still not working and now I get a new error: Exception in thread "main" java.lang.Error: Unresolved compilation problem:
at test.main(test.java:13)
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, 07:44 AM #9
Re: my command-line argument doesn't pass values!
So without really understanding what the problem is you went and made wholesale changes and made your code worse. I told you that you were creating local variables in your validateInput method. If you do not understand what I mean by local variables then you should go and read about it.
Here is an example of what you are doing.
Java Code:class LocalVariables { private int fred = 0; private int barney = 0; LocalVariables(int f, int b) { fred = f; int barney = b; } public void printStuff() { System.out.println(fred); System.out.println(barney); } public static void main(String[] args) { LocalVariables test = new LocalVariables(10, 20); test.printStuff(); } }
10
0
- 10-16-2012, 07:55 AM #10
Member
- Join Date
- Sep 2012
- Posts
- 26
- Rep Power
- 0
Re: my command-line argument doesn't pass values!
Okay, I don't understand relationship between my array and the command line. this is the reason i can't fix it. I 'll check your code and let you know.. Thank you :)
- 10-16-2012, 07:58 AM #11
Re: my command-line argument doesn't pass values!
FFS
This has nothing to do with the command line or arrays. IT HAS TO DO WITH THE FACT THAT IN YOUR VALIDATEINPUT METHOD YOU HAVE CREATED LOCAL VARIABLES FOR B AND C.
Did you look at the above example?
Do you see why the output for barney is 0 and not 20?
Did you do as I initially suggested and go back to your original code and see what difference there is between the a variable and the b and c variables?
- 10-16-2012, 08:12 AM #12
Member
- Join Date
- Sep 2012
- Posts
- 26
- Rep Power
- 0
Re: my command-line argument doesn't pass values!
yes, I see what you mean, and I changed it , but still get stupid errors. but I think there must be something else wrong with my code! I thing its my array and my methods...
I wanna blow my head...
- 10-16-2012, 08:13 AM #13
Re: my command-line argument doesn't pass values!
Maybe I should make this as simple as I can. Here are the 3 important lines of code from your validateInput method:
Java Code:a= Integer.parseInt(num1); // GOOD int b= Integer.parseInt(num2); // BAD int c= Integer.parseInt(num3); // BAD
- 10-16-2012, 08:14 AM #14
Re: my command-line argument doesn't pass values!
We cannot see your computer screen. You need to post your updated code and the full and exact error messages.
- 10-16-2012, 08:32 AM #15
Member
- Join Date
- Sep 2012
- Posts
- 26
- Rep Power
- 0
Re: my command-line argument doesn't pass values!
okay here is what I want to do: I should get 3 int in my command line.
Perform the following tests for each of the 3 command line arguments
a. Is the value a valid integer?
Use the Integer.parseInt() method and try…catch blocks
If not, display the error message “Input #n is not a valid integer”
1. Where n is the number of the respective value (1, 2 or 3)
and here is what is did only for one command line:
by the way with your help I am able to send the value to the method, but my methods don't work properly!!
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 [1]; for(int i =0; i< myNumbers.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[i]); if(!withinRange(array)) { isValid = true; } } } catch(Exception ex) { System.out.println("The entered value " + a + " is not a valid integer. Please try again."); } return isValid; } //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[0]) +" is out of range [1 TO 1000]."); isInRange = false; } } return isInRange; } /** * 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 } }
Last edited by niloufar; 10-16-2012 at 08:41 AM.
- 10-16-2012, 08:59 AM #16
Member
- Join Date
- Sep 2012
- Posts
- 26
- Rep Power
- 0
Re: my command-line argument doesn't pass values!
here is my result:
The entered value 0 is not a valid integer. Please try again. """ Wrong"""
but my number is 0 and this is an integer. it has to catch the error and tell me that my entry is not within range!
- 10-16-2012, 11:12 AM #17
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 26
Re: my command-line argument doesn't pass values!
Why are you creating an empty array and using that as your parameter for validateInput?
Surely it's the command line arguments you want to use?
Your original code was a lot closer than this.
All you need to do was put in some debugging and actually see what values were being used and where.
That involves sticking lots and lots of System.out.println statements (at least one for every time a variable changes), showing where in the code you are and what the values of certain variables are.
Then you can show us that code here, including the output.Please do not ask for code as refusal often offends.
** This space for rent **
- 10-17-2012, 08:04 AM #18
Member
- Join Date
- Sep 2012
- Posts
- 26
- Rep Power
- 0
Similar Threads
-
"pass argument:what does it mean?" can you pls read and give me some feedback
By edelric666 in forum New To JavaReplies: 8Last Post: 11-05-2010, 12:03 AM -
list values in a number of steps given as a command line argument
By Shyamz1 in forum New To JavaReplies: 4Last Post: 10-31-2010, 06:19 PM -
HashMap contains all values but doesn't show all values
By xcallmejudasx in forum New To JavaReplies: 3Last Post: 05-11-2009, 12:35 AM -
Parsing Argument Values
By vipvan2000 in forum New To JavaReplies: 1Last Post: 03-06-2008, 12:01 PM -
Parsing Argument Values
By vipvan2000 in forum Advanced JavaReplies: 1Last Post: 02-17-2008, 02:41 AM
Bookmarks