Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3305988
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:14:06+00:00 2026-05-17T21:14:06+00:00

import java.util.regex.*; import java.io.*; class Patmatch{ static String str = ; public static void

  • 0
import java.util.regex.*;
import java.io.*;
class Patmatch{

    static String str = "";

    public static void main(String[] args){
        BufferedReader br =
            new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter name to see match");
        try{

            str = br.readLine();
        } catch(IOException e){
            System.out.println("Exception has been occurred" + e);

        }

        try{
            Patternmatch();
        } catch(NomatchException me){
            System.out.println("Exception" + me);
        }
    }

    private static void Patternmatch() throws NomatchException{

        Pattern p = Pattern.compile("ab");
        Matcher m = p.matcher(str);
        while(m.find())
            System.out.print(m.start() + " ");

        throw new NomatchException("no match");

    }
}

class NomatchException extends Exception{

    NomatchException(String s){
        super(s);
    }
}

In the above code when i enter ab it shows the position exaclty as 0.But is also shows exception. I need output like if i enter ab it should show ab. if i enter something else like def it must show exception. Can you please help me?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T21:14:07+00:00Added an answer on May 17, 2026 at 9:14 pm

    Here’s the changed method:

    private static void patternMatch() throws NomatchException{
    
        final Pattern p = Pattern.compile("ab");
        final Matcher m = p.matcher(str);
    
        if(m.find()){
            System.out.print(m.group());
        } else{
            throw new NomatchException("no match");
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

import java.util.scanner; import javax.swing.JOptionPane; public class FirstHomeJavaApplet{ public static void main(String[] args){ int num1=2;
Consider this code: import java.util.regex.*; public class Pattern3 { /** * @param args the
import java.util.*; import org.directwebremoting.util.Logger; public class People { public People() { people = new
Consider the following program: import java.util.List; import java.util.ArrayList; public class TypeTest { public static
Take the following generics example import java.util.List; import java.util.ArrayList; public class GenericsTest { private
import java.util.ArrayList; import java.util.Arrays; public class Cloud { private ArrayList<Point> points; private double left;
package myProg; import java.util.Scanner; public class oving4a { /** *Øving 4a. Aleksander Pettersen */
package timeToys; import java.util.regex.Pattern; ** * A DayTime is an immutable object that stores
I have three programs, first does a selenium test import com.thoughtworks.selenium.*; import java.util.regex.Pattern; import
i have created synchronized arrayList like this import java.text.SimpleDateFormat; import java.util.*; class HelloThread {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.