Java如何从文本文件中搜索单词
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20303944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java How to search a word from text file
提问by Jimmy Bob
edit: Write a program to read in 100 words from a file. Then, have the user search for a word until they enter 'quit'. The program will read in up to 100 words from a file. The file may or may not contain 100 words but the array should hold up to 100 (if the list does not contain enough words, fill the rest of the array with empty strings). After the file is read in, the program will prompt the user for a search string. The program will then search for the string and tell the user if the word was found or not. The program will continue to get search strings from the user until the user enters 'quit'
编辑:编写一个程序从文件中读取 100 个字。然后,让用户搜索一个词,直到他们输入“退出”。该程序将从文件中读取最多 100 个单词。该文件可能包含也可能不包含 100 个单词,但该数组最多可容纳 100 个(如果列表不包含足够的单词,则用空字符串填充数组的其余部分)。读入文件后,程序将提示用户输入搜索字符串。然后程序将搜索字符串并告诉用户是否找到了该词。程序将继续从用户那里获取搜索字符串,直到用户输入“退出”
Hello I need help write a program to find a word from text file
你好,我需要帮助编写一个程序来从文本文件中找到一个单词
the result should look like:
结果应如下所示:
Enter a word to search for: taco
输入要搜索的词:taco
Word 'taco' was found.
发现了“taco”这个词。
Enter a word to search for: asd
输入要搜索的词:asd
Word 'asd' was NOT found.
未找到单词“asd”。
and when user enter the word "quit" the program will quit
当用户输入“退出”这个词时,程序将退出
below is what I have so far and need help to complete
以下是我到目前为止所拥有的,需要帮助才能完成
import java.io.*;
import java.util.Scanner;
public class project2 {
public static void main( String[] args ) throws IOException {
String[] list;
String search;
list = load_list( "words.txt" );
search = prompt_user( "\nEnter a word to search for: " );
while ( ! search.equals( "quit" ) ) {
System.out.println( "Word '" + search + "' was" +
( ( find_word( search, list ) ) ? "" : " NOT" ) +
" found." );
search = prompt_user( "\nEnter a word to search for: " );
}
System.out.println();
}
回答by ghostbust555
for(String s: list){
if(s.equals(search)){
//do whatever
}
}
回答by Prashant Ghimire
Use this:
用这个:
Scanner txtscan = new Scanner(new File("filename.txt"));
while(txtscan.hasNextLine()){
String str = txtscan.nextLine();
if(str.indexOf("word") != -1){
System.out.println("EXISTS");
}
}
回答by Aarish Ramesh
The below code answers this question perfectly:
下面的代码完美地回答了这个问题:
String word = ""; int val = 0;
while(!word.matches("quit"))
{
System.out.println("Enter the word to be searched for");
Scanner input = new Scanner(System.in);
word = input.next();
Scanner file = new Scanner(new File("newFile.txt"));
while(file.hasNextLine())
{
String line = file.nextLine();
if(line.indexOf(word) != -1)
{
System.out.println("Word EXISTS in the file");
val = 1;
break;
}
else
{
val = 0;
continue;
}
}
if(val == 0)
{
System.out.println("Word does not exist");
}
System.out.println("-------continue or quit--- enter continue or quit");
word = input.next();
}