简体   繁体   English

Java中的模式/数组问题

[英]Pattern/Array issue in java

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]);
            }
        }
    }
}
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? 您的循环总共访问多少个情感?

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

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

Try 尝试

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

and you'll be fine :) 你会没事的:)

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

as you your own in the next for loop in your code has used < instead of <= 因为您自己在代码的下一个 for循环中使用了<而不是<=

Try this. 尝试这个。 It works. 有用。

Tips: Make sure you use nextLine() so that the input will read full sentences. 提示:确保使用nextLine(),以便输入将阅读完整的句子。 And I switched your while loop for an if statement within the for loop. 我在for循环内将while循环切换为if语句。 No need for two loops there. 那里不需要两个循环。 And I also condensed your first for loop to just one line. 而且我还将您的第一个for循环压缩为仅一行。 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++;
            }
        }
    }
}

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

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