import java.io.*;
class multiexp { public static void main(String args[]) { int arr[]={100,200,300,400,500}; System.out.println("Enter a number as array index and find out its value"); System.out.println("Enter end to come out of the program"); try{ String line; int x; BufferedReader d=new BufferedReader(new InputStreamReader(System.in)); while((line=d.readLine())!=null) { if(line.equals("end")) break; else { try { x=Integer.parseInt(line); System.out.println("Valid element and it is: "+arr[x]); }catch(ArrayIndexOutOfBoundsException e) { System.out.println("Invalid Element"); System.out.println("Exception generated: "+e); }catch(NumberFormatException n) { System.out.println("Sorry no characters"); System.out.println("Generated exceptino: "+n); } } } }catch(IOException i){} } } OutPut: Enter a number as array index and find out its value Enter end to come out of the program 4 Valid element and it is: 500 6 Invalid Element Exception generated: java.lang.ArrayIndexOutOfBoundsException: 6 3 Valid element and it is: 400 end |
0 comments:
Post a Comment