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 8343167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:58:34+00:00 2026-06-09T05:58:34+00:00

I am studying for an upcoming test next month and looking at some basic

  • 0

I am studying for an upcoming test next month and looking at some basic problems. This one is a program that requires entering a few sentences and reprinting any that contain a certain string, ‘pattern’ in this case.

My attempt is below and it compiles however I receive the following error when trying to run it:

   Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at Grep.main(Grep.java:18)
import java.util.Scanner;
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class Grep {

    public static void main(String[] args) {

        Pattern pattern = Pattern.compile("[Pp]attern");
        String sentences[] = new String[10];
        Scanner scanner = new Scanner(System.in); 

        System.out.println("Please enter some sentences: ");

        for (int i = 0; i <= sentences.length; i++) {
            String s = scanner.next(); 
            sentences[i] = s;
        }

        for (int i = 0; i < sentences.length; i++) { 
            Matcher matcher = pattern.matcher(sentences[i]);
            while (matcher.find()) {
                System.out.println(sentences[i]);
            }
        }
    }
}
  • 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-06-09T05:58:37+00:00Added an answer on June 9, 2026 at 5:58 am

    Try this. It works.

    Tips:
    Make sure you use nextLine() so that the input will read full sentences. And I switched your while loop for an if statement within the for loop. No need for two loops there. And I also condensed your first for loop to just one line. No need to create a string variable if you only need it for a second. Just skip that step entirely and get to the point! Good luck, Hope this helps!

    Below Is A Program That Mirrors Yours But Now Works

    import java.util.Scanner;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class Grep 
    {
        public static void main(String[] args) 
        {
            Pattern pattern = Pattern.compile("[Pp]attern");
            String sentences[] = new String[3];
            Scanner scanner = new Scanner(System.in);
    
            System.out.println("Please enter some sentences: ");
    
            for (int i = 0; i < sentences.length; i++) 
                sentences[i] = scanner.nextLine();
    
            for (int i = 0; i < sentences.length; i++) 
            { 
                Matcher matcher = pattern.matcher(sentences[i]);
                if (matcher.find()) 
                    System.out.println(sentences[i]);
            }
        }
    } 
    

    Below Is How I Would Write This Same Program. Comments Included For Clarification

    import java.util.Scanner;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class Grep 
    {
        public static void main(String[] args) 
        {
            // Initialize and Declare Variables
            Pattern pattern = Pattern.compile("[Pp]attern");
            String sentences[] = new String[3];
            Scanner scanner = new Scanner(System.in);
            int foundCount = 1;
    
    
            // Present A Title For The End User
            System.out.println("This Program Will Catch Sentences With The Term Pattern.\n");
    
    
            // Read The Inputs From The Users
            for (int i = 0; i < sentences.length; i++)
            {
                System.out.print("Enter Sentence #" + (i+1) + ":  ");
                sentences[i] = scanner.nextLine();
            }
    
    
            // Line Break
            System.out.println();
    
    
            // Write Sentences That Include The Term Pattern
            for (int i = 0; i < sentences.length; i++) 
            { 
                Matcher matcher = pattern.matcher(sentences[i]);
                if (matcher.find())
                {
                    System.out.println(foundCount + ")  " + sentences[i]);
                    foundCount++;
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

while studying some security things, there was a question that one can guess the
I'm studying for upcoming interviews and have encountered this question several times (written verbatim)
While studying the Collection API, we find that some methods ( add , remove
Im studying some ruby code and I see this varx variable being used with
I'm studying for an exam, and this is a problem from an old test:
I have been studying signals in Linux. And I've done a test program to
While studying about JMX, I have seen one of the important feature of it
While studying Java tutorials, Reflection and Late Binding have confused me. In some tutorials,
I started studying Haskell one week ago and have one strange problem. I created
I am studying an existing Perl program, which includes the following line of code:

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.