-1
import java.util.*;
import java.io.*;
public class search {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner x = null;
        String k;
        int c= 0;
        try{
                 x =new Scanner( new BufferedReader (new FileReader("hh.txt"))); 
        while(x.hasNext()){
             k = x.next();
            if(k.equals(args[1]))
             {
               c++; 
             }
        }
        }catch(Exception ex){
            System.out.println(ex.toString());
        }
System.out.println("The number of occurrence of String is "+c);
}
}

The Program throws a Exception can any one tell me how to handle it. java.lang.ArrayIndexOutOfBoundsException: 0 The number of occurrence of String is 0

Asad
  • 11
  • 4
  • 2
    Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](http://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – JFPicard Nov 26 '15 at 15:12
  • 2
    Possible reason is that you run your program without any arguments, so calling args[1] cause exception. – Edgar Rokjān Nov 26 '15 at 15:13
  • Don't catch the exception - you are losing valuable information by just printing it, such as the line on which the exception occurred. – assylias Nov 26 '15 at 15:13
  • use `ex.printStackTrace()` instead of `System.out.println(ex.toString())` to get the correct exception – Blip Nov 26 '15 at 15:13
  • 2
    I suspect you want to use `if(k.equals(args[0]))` instead of `if(k.equals(args[1]))`. – Manu Nov 26 '15 at 15:18
  • Are you running it on an IDE? Run it from console as: `java search arg1 arg2 ...` Also check [Java naming conventions](http://www.oracle.com/technetwork/java/codeconventions-135099.html): *Class names should start with an upper case* – Frakcool Nov 26 '15 at 15:21

2 Answers2

0

Please check

if ( args[1] != null ) {

}

before using args[1] like below

if ( args[1] != null ) {
     if ( k.equals(args[1]) ) {
           c++; 
     }
}
Prakash Thete
  • 3,802
  • 28
  • 28
0

Simply catching an exception like this is a very bad thing to do.

   } catch(Exception ex){
        System.out.println(ex.toString());
   }

That there is an exception indicates that something is wrong and the default action that happens will be to terminate the thread with the exception message and a stack trace indicating where the exception occurred.

If you catch the exception and continue as you do here, bad and unexpected things can happen in the remainder of your program. So don't catch exceptions you are not expecting.

Instead catch specific exceptions - such as ArrayIndexOutOfBoundsException - which you think might happen and take the appropriate action. Or put in a test to avoid the exception occurring, like checking that the appropriate arguments have been passed in. Never catch unexpected exceptions and continue execution

Neil Masson
  • 2,609
  • 1
  • 15
  • 23