gpt4 book ai didi

java - 扫描仪和 .hasNext() 问题

转载 作者:搜寻专家 更新时间:2023-11-01 02:05:56 26 4
gpt4 key购买 nike

我是 Java 的新手,也是 Scanner 类的新手。我正在编写一个程序,它要求用户输入一个词,然后在一个文件中搜索这个词。每次找到该单词时,它都会打印在 JOptionPane 中的新行上,以及它前后的单词。一切正常,但有两个异常(exception):

  1. 如果要搜索的词恰好是文件中的最后一个词,则会抛出“NoSuchElementException”。

  2. 如果正在搜索的词连续出现两次(不太可能,但仍然是我发现的问题),它只会返回一次。例如,如果要搜索的词是“had”,“He said that he had had had had had enough. He had been up all night”是文件中的句子,则输出为:

    he had had
    He had been

    而它应该是:

    he had had
    had had enough.
    He had been

我认为我的问题在于我使用了 while(scan.hasNext()) 并且在这个循环中我使用了两次 scan.next() .虽然我找不到解决方案,但仍能实现我希望程序返回的结果。

这是我的代码:

//WordSearch.java
/*
* Program which asks the user to enter a filename followed
* by a word to search for within the file. The program then
* returns every occurrence of this word as well as the
* previous and next word it appear with. Each of these
* occurrences are printed on a new line when displayed
* to the user.
*/

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JOptionPane;

public class WordSearch {

public static void main(String[] args) throws FileNotFoundException {

String fileName = JOptionPane.showInputDialog("Enter the name of the file to be searched:");
FileReader reader = new FileReader(fileName);

String searchWord = JOptionPane.showInputDialog("Enter the word to be searched for in \"" + fileName + "\":");
Scanner scan = new Scanner(reader);

int occurrenceNum = 0;
ArrayList<String> occurrenceList = new ArrayList<String>();
String word = "", previousWord, nextWord = "", message = "", occurrence, allOccurrences = "";

while(scan.hasNext()){
previousWord = word;
word = scan.next();

if(word.equalsIgnoreCase(searchWord)){
nextWord = scan.next();

if(previousWord.equals("")){
message = word + " is the first word of the file.\nHere are the occurrences of it:\n\n";
occurrence = word + " " + nextWord;
}
else{
occurrence = previousWord + " " + word + " " + nextWord;
}

occurrenceNum++;
occurrenceList.add(occurrence);
}
}

for(int i = 0; i < occurrenceNum; i++){
allOccurrences += occurrenceList.get(i) + "\n";
}

JOptionPane.showMessageDialog(null, message + allOccurrences);

scan.close();
}
}

此外,附带说明:我如何实现 scan.useDelimeter() 以忽略任何问号、逗号、句号、撇号等?

最佳答案

If the word being searched for happens to be the last word in the file then a NoSuchElementException is thrown.

这是因为这一行:

if(word.equalsIgnoreCase(searchWord)) {
nextWord = scan.next();
...
}

你不检查 scan实际上hasNext() , 直奔 scan.next() .您可以通过调用 scan.hasNext() 添加条件来解决此问题

If the word being searched for appears twice in a row (unlikely, but still a problem I discovered), it only returns it once.

这里也存在同样的问题:当您找到一个词时,您会立即检索下一个词。

解决这个问题有点棘手:您需要将算法更改为一次查看一个单词,并使用 previousWord (无论如何你都存储)用于 while 的后续迭代循环。

关于java - 扫描仪和 .hasNext() 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33442483/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com