다음 달에 시험을 치러 몇 가지 기본적인 문제를 조사하고 있습니다. 이것은 하나의 문장으로 몇 문장을 입력하고 어떤 경우에는 '패턴'을 포함하는 문장을 다시 인쇄해야하는 프로그램입니다.자바에서 패턴/배열 문제가 발생했습니다
내 시도는 아래 내가 그것을 실행하려고 다음과 같은 오류가 나타납니다 그러나 그것은 컴파일 :
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]);
}
}
}
}
도움 주셔서 대단히 감사합니다. 명령 줄에서 검색어를 호출하는 방법에 대한 도움말 예를 들어 입력에서 패턴을 검색하고 싶다면 'Java Grep pattern'을 호출하십시오. – BLL27