How can i give an error depending on the array input?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lociteck
    New Member
    • Apr 2013
    • 1

    How can i give an error depending on the array input?

    Hello,
    How can i give an error depending on the array input? This is my code

    Code:
    using System;
    class MainClass
    	
    {
    		public static void Main (string[] args)
    		{
    		int[] randomSizedArray;
    		string sizeOfArray;
    		int convertedSizeArray = -1;
    		Console.WriteLine ("Please Enter the Size of the Array Between 1-99");
    		sizeOfArray = Console.ReadLine();
    		convertedSizeArray = Int32.Parse(sizeOfArray);	
    		randomSizedArray= new int[convertedSizeArray];
    		Random rnd = new Random();
    		for (int i=0; i < convertedSizeArray; i++) {
    			
        	randomSizedArray[i] = rnd.Next(1,99);
    		
    			
    		}
    			for (int i=0; i < convertedSizeArray; i++) 
    {
        Console.WriteLine(randomSizedArray[i] + "");
    			
    }
    		string swapindex1;
    		string swapindex2;
    		int index1;
    		int index2;
    		Console.WriteLine("Please Enter Index to swap");
    		swapindex1 = Console.ReadLine();
         	index1 =  Int32.Parse(swapindex1);
    		int temp = randomSizedArray[index1];
    		Console.WriteLine ("Please Enter a Second Value to swap");	
    		swapindex2 = Console.ReadLine();
         	index2 =  Int32.Parse(swapindex2);	
    		randomSizedArray[index1] = randomSizedArray[index2];
    		randomSizedArray[index2] = temp;
    		 for (int j=0; j < convertedSizeArray; j++){
    			Console.WriteLine(randomSizedArray[j]);
    	}
    		
    	}	
    		
     
    }
    So im going to give an error if the user inputs different number besides 1-99, please help you guys, i've never done any error coding.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Use an if to check whether or not the inputted value is within your range. Throw an error if it is outside the range.

    Comment

    • bharathreddy
      New Member
      • Aug 2006
      • 116

      #3
      Rabbit is right, keep a if check before forloop on sizeOfArray variable. If it falls outside the allowed range then throw error message to the user.


      Bharath Vasireddy

      Comment

      Working...