0

我正在为下个月即将到来的考试而学习,并研究一些基本问题。这是一个程序,需要输入几个句子并重新打印包含特定字符串的任何句子,在这种情况下为“模式”。

我的尝试如下,它编译但是我在尝试运行它时收到以下错误:

   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]);
            }
        }
    }
}
4

5 回答 5

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

数组中有多少项?最后的索引是什么?您的循环使用的最后一个索引是什么?您的循环总共访问了多少句?

于 2012-08-02T05:27:55.540 回答
0
for (int i = 0; i <= sentences.length; i++) {

<=需要是<因为你从 0 开始并且你有 10 个项目,所以必须i从 0 到 9。

于 2012-08-02T05:28:08.083 回答
0

尝试

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

你会没事的:)

于 2012-08-02T05:29:01.520 回答
0

问题出在:18您的代码中for (int i = 0; i <= sentences.length; i++),应该是for (int i = 0; i < sentences.length; i++)

正如您在代码中的下一个 for循环中使用<的那样,而不是<=

于 2012-08-02T05:33:02.640 回答
0

试试这个。有用。

提示:确保使用 nextLine() 以便输入将读取完整的句子。我将您的 while 循环切换为 for 循环中的 if 语句。那里不需要两个循环。而且我还将您的第一个 for 循环压缩为一行。如果您只需要一秒钟,则无需创建字符串变量。完全跳过这一步,直奔主题!祝你好运,希望这会有帮助!

下面是一个反映你但现在可以工作的程序

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]);
        }
    }
} 

下面是我将如何编写这个相同的程序。包含用于澄清的评论

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++;
            }
        }
    }
}
于 2012-08-02T05:34:39.013 回答