tags:

views:

75

answers:

3
class Patmatch {
  static String strLine="";

  public static void main(String [] args) {    
    try {
      FileInputStream fstream = new FileInputStream("c://abc.txt");
      DataInputStream in = new DataInputStream(fstream);
      BufferedReader br1 = new BufferedReader(new InputStreamReader(in));           
      while ((strLine = br1.readLine()) != null) {
        try {
          Patternmatch();
        }
        catch(NomatchException me) {
          System.out.println(me.getMessage());
        }
      }
      in.close();
    }
    catch(IOException i) {
      System.out.println("exec:"+i);
    }   
  }

  private static void Patternmatch() throws NomatchException {
    Pattern p = Pattern.compile("---"); 
    Matcher m = p.matcher(strLine);  
    if(m.find()) { 
      System.out.print(m.start()); 
    }
    else { 
      throw new NomatchException("no match"); 
    } 
  }
}

class NomatchException extends Exception {
  NomatchException(String s) {
    super(s);
  }
}

In the above code Exception no match is printed twice.

I have abc.txt where i don't have the pattern ---.

I can understand that in that main i have caught this exception and also in NomatchException() i have thrown but if i remove the try...catch it shows error and if the remove in nomatchException() nothing s displayed.

What should I do now to correct it?

+2  A: 

You are only calling System.out.print() once, but you are doing it in a while loop. Perhaps you have extra lines at the end of abc.txt which cause your loop to continue.

EDIT in response to comment:

What shoud i do even if i have so many lines exception should be printed once but if i have --- in many places all those positions must be printed?

First - please note that Exceptions should not be used to control the flow of your program - generally, they are meant to only happen in exceptiional circumstances.

Now, if you want to indicate that you found at least 1 non-matching line in abc.txt, then you can redesign your program such that your Patternmatch() will return a boolean result, and based on that return value you can increment a new errorCnt field. If this new counter is greater than zero, you can print your informational message.

If, however, you would like to simply omit the error from appearing for empty lines in text, you can put a simple test in your Patternmatch() method to test the length of the trim()'d string:

  if (strLine.trim().length() > 0 ) {
      //do your test
  }

Also - a note on naming in Java. Class names start with a capital letter (so nomatchException should be NomatchException) and method names start with a lowercase letter (Patternmatch() should be patternmatch()).

akf
ya i tried removing the second line and now only once its displayed. What shoud i do even if i have so many lines exception should be printed once but if i have --- in many places all those positions must be printed?
Sumithra
A: 

I agree with akf - exceptions should be exceptional. Another way to control your logic flow could be:

while (((strLine = br1.readLine()) != null)) {
   if (!Patternmatch())
      System.out.println("no match");
}

and modifying your Patternmatch()

private static boolean Patternmatch() throws NomatchException {       
   Pattern p = Pattern.compile("---");        
   Matcher m = p.matcher(strLine);         

   return m.find();
}
Michael
will void return value as per your code?
Sumithra
Whoops! thanks- edited to boolean.
Michael
A: 

You must have two lines in your text file that don't match.

As the others have said, this is a poor use of exceptions. Have the patternMatch() method return a boolean.

EJP