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]);
 }
 }
 }
}
Edwin Dalorzo
78.9k25 gold badges151 silver badges211 bronze badges
asked Aug 2, 2012 at 5:25
0

5 Answers 5

3
for (int i = 0; i <= sentences.length; i++) {

How many items are in the array? What is the last index? What is the last index your loop uses? How many sentances in total does your loop access?

answered Aug 2, 2012 at 5:27
Sign up to request clarification or add additional context in comments.

Comments

0

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++;
 }
 }
 }
}
answered Aug 2, 2012 at 5:34

1 Comment

Thank you very much for your help. Any tips on how I would invoke the search term from the command line? Eg if I wanted to search for pattern in the input and invoked 'java Grep pattern'.
0
for (int i = 0; i <= sentences.length; i++) {

The <= needs to be < because you are starting from 0 and you have 10 items, so i has to go from 0 to 9.

answered Aug 2, 2012 at 5:28

1 Comment

Awesome, completely missed that. Thank you.
0

The problem is in line :18 of your code which is for (int i = 0; i <= sentences.length; i++) it should be for (int i = 0; i < sentences.length; i++)

as you your own in the next for loop in your code has used < instead of <=

answered Aug 2, 2012 at 5:33

1 Comment

Java generally uses 0-based indexes, so the last element will be at position (size - 1).
0

Try

for (int i = 0; i < sentences.length; i++)

and you'll be fine :)

sloth
101k21 gold badges183 silver badges224 bronze badges
answered Aug 2, 2012 at 5:29

1 Comment

Awesome, completely missed that. Thank you.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.