簡體   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