繁体   English   中英

Java中的模式/数组问题

[英]Pattern/Array issue in java

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

我的尝试在下面,并且可以编译,但是尝试运行时收到以下错误:

  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]);
            }
        }
    }
}
for (int i = 0; i <= sentences.length; i++) {

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

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

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

尝试

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

你会没事的:)

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

因为您自己在代码的下一个 for循环中使用了<而不是<=

尝试这个。 有用。

提示:确保使用nextLine(),以便输入将阅读完整的句子。 我在for循环内将while循环切换为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++;
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM