Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

How to do specific Parameter checking

$
0
0
Hey everyone, I have and odd checking request, that I do not really know how to solve. In my program I am wanting to enter parameters from the commands line (I know how to do this) they have to (or should be) be integers. I am writing a program that allows me to benchmark adding random integers and random strings (of random sizes) into two different arrays; the sizes will be determined by the command line parameters. For my random generator I have it so I can create a generator with 2, 1, or no arguments at all. So my issue is that I dont know how to check for the multiple arguments (see comments below in code for what I mean).

Can someone please check to make sure I have this all right in terms of checking for the arguments?

private static void checkArgs(String[] arg){
		// The length of the args will be at least 1, due to the program name
		if(arg.length==1){ // There are no external arguments that the user entered in
			rg=new RandGen(); // Set the bounds to DEFAULT_SIZE
		}else if(arg.length==2){ // The user entered in only one argument, this will be used for the Upper Bound on the random integers
			// Need to check if the argument that the user has entered an integer
			try{ // This will determine if the user has entered in a parsable number
				int n= Integer.parseInt(arg[1]);
				rg=new RandGen(n); //  Set the random integer bound to what the user entered in
			}catch(NumberFormatException nfe){
				rg=new RandGen(); // If the number can not be parsed, then use the default constructor
			}
		}else if(arg.length>=3){ // If the user has entered in more than 2 or more arguments
			try{ // Check the first two user entered elements, if the user has entered in more than 2, they will be ignored
				 // If the user has entered in un-parseable elements as the first two, and has enter more elements than required, then will use the parsable elements
				 // If the user has entered one parseable element, and one non-parseable element, will default to previous case, provided that there are no other elements
				 // If the user has entered no parseable elements, then the first case with no elements will be used
				int n=0, s=0; // Bounds for the random integer and the string size respectively
				int count=0; // Count for if 2 of the arguments are parseable
				String temp;
				for(int i=1;i<arg.length;i++){
					temp=arg[i];
					for(int j=0;j<temp.length();j++){
						// Check to make sure that there is a digit here
						if(Character.isDigit(temp.charAt(j))){
							if(j==temp.length()-1){
								count++;
							}
						}else {
							break; // Break out of the loop if there are invalid entries
						}
					}
					if(count==1){
						n=Integer.parseInt(arg[i]);
					}
					// Check if count is 2, so that two valid elements
					else if(count==2){
						s=Integer.parseInt(arg[i]);
						break;
					}
				}
				if(n==0 && s==0){ // If both of the arguments are either 0 or if there was not any parseable numbers, default to the no arguments case
					rg=new RandGen();
				}else if(n!=0 && s==0){ // If there was one parseable number
					rg= new RandGen(n);
				}else if(n!=0 && s!=0){ // Both arguments are valid
					rg= new RandGen(n,s);
				}
			}catch(NumberFormatException nfe){
				// This place may be not used, since the checkin is done above
			}
		}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>