簡體   English   中英

java-如何在令牌中存儲文件中的拆分句子(ArrayIndexBoundException錯誤為0)java?

[英]How to store split sentences from a file in tokens (ArrayIndexBoundException error at 0) java?

我是Java的新手,正在從名為a2b.txt的文件中讀取句子,並且該文件包含類似這樣的句子。

Read in the lines of text, process them into words, and store them into an array (or, optionally, an ArrayList) of objects of your Sentence class. Each Sentence object should start out empty, and have words added to it as they are received from the file. Your Sentence class must contain at least the following. If you are storing the sentences in an array, you can assume that the text file contains at most 1000 sentences. Once you have read in the contents of the file, process it in the following way!

首先,我用定界符.!分割了文件.! 當我遇到arrayIndexBoundException錯誤時,我想將每個句子讀入各種對象。 這是我的代碼。

BufferedReader input;
    String text;
    String[] words;
    String sentence1,sentence2, sentence3,sentence4,sentence5;

    sentence[] sentences = new sentence[1000];
    int size = 0;

    try{
        input= new BufferedReader(new FileReader("a2b.txt"));

        text =input.readLine();
        while (text!=null){
            words = text.split(".|!");

            sentence1 = words[0].trim();
            sentence2 = words[1].trim();
            sentence3 = words[2].trim();
            sentence4 = words[3].trim();
            sentence5 = words[4].trim();

            sentences[size] = new sentence(sentence1);

            //for(int x =0; x< words.length;x++){
                //System.out.println(words[x]);

            //}
            text =input.readLine();
        }
        input.close();

    }catch (ArrayIndexOutOfBoundsException | IOException ex){
        System.out.println(ex.getMessage());
    }

    for(int i=0; i<size;i++){
        System.out.println(sentences[i]);
    }
}

我在0處收到arrayIndexBountException。我希望我的句子在每個`。!之后逐行打印出來。 。任何幫助如何獲得通過將不勝感激。

最后,我使用ArrayList而不是掃描程序類解決了我的問題。

    List<String> sentences = new ArrayList<>();
    //int size =0;


    try{

        File file = new File("a2b.txt");
        Scanner scanner = new Scanner(file);
        scanner.useDelimiter("[.?!]");
        while(scanner.hasNext()){
            EachSentence = scanner.next();
            EachSentence = EachSentence.replaceAll("\\r?\\n", " ");
            EachSentence = EachSentence.trim();
            sentences.add(EachSentence);
        }
        scanner.close();

    }catch (IOException ex){ 
        System.out.println(ex.getMessage());
    }

這按我的預期打印了行。

Read in the lines of text, process them into words, and store them into an array (or, optionally, an ArrayList) of objects of your Sentence class.

Each Sentence object should start out empty, and have words added to it as they are received from the file.

Your Sentence class must contain at least the following.

If you are storing the sentences in an array, you can assume that the text file contains at most 1000 sentences.

Once you have read in the contents of the file, process it in the following way.

使用

for(int i=0;i<sentences.size();i++){
      System.out.println("\n"+sentences.get(i)+".");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM