java – Scanner&.hasNext()问题
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – Scanner&.hasNext()问题,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2928字,纯文字阅读大概需要5分钟。
内容图文
![java – Scanner&.hasNext()问题](/upload/InfoBanner/zyjiaocheng/819/448495a3268c4192b744884b9b769e92.jpg)
我是Java的新手,也是Scanner类的新手.我正在编写一个程序,要求用户输入一个单词,然后在一个文件中搜索这个单词.每次找到该单词时,它都会打印在JOptionPane中的新行以及它之前和之后的单词上.一切都按预期运作,但有两个例外:
>如果搜索的单词恰好是文件中的最后一个单词,则抛出“NoSuchElementException”.
>如果被搜索的单词连续出现两次(不太可能,但仍然是我发现的问题),它只返回一次.例如,如果被搜索的单词是“had”和“他说他已经受够了.他整晚都在上面”是文件中的句子,那么输出是:
he had had
He had been
而它应该是:
he had had
had had enough.
He had been
我相信我的问题在于我使用了一段时间(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();
...
}
您不检查扫描是否实际上是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 – Scanner&.hasNext()问题全部内容,希望文章能够帮你解决java – Scanner&.hasNext()问题所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。