【发布时间】:2017-12-09 23:15:40
【问题描述】:
我有一组字符串 (string1,string2,string3,string4),我想使用 java 编码在文本文件 (input.txt) 中搜索它,我尝试使用以下命令但它不起作用,请帮助.
package string;
import java.io.File; //Required for input file
import java.io.FileNotFoundException; //Required for exceptioenter code heren throw
import java.util.Scanner; //required for scanner
public class strng {
public static void main(String[] args) throws FileNotFoundException // throws clause added
{
//ask the the for the string to be searched
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter part of the string: ");
String searchString = keyboard.next().toLowerCase();
// open the data file
File file = new File("APPD_01_15_16_01.txt");
// create a scanner from the file
Scanner inputFile = new Scanner (file);
// While there is another line to read.
while(inputFile.hasNext())
{
// read the lines
//Read string
//Check if user input is a match and if true print out info.
if(searchString.contains("samplemachine")
{
System.out.println("Yup!");
}
else
{
System.out.println("Fail!");
}
}
// be polite and close the file
inputFile.close();
}
}
【问题讨论】:
-
您从 inputFile 中读取的字符串在哪里?
-
您的示例代码有点令人困惑,因为您似乎将用户键盘输入与您搜索“samplemachine”的字符串进行比较,而不是文件中的输入。
-
我尝试从用户输入中搜索,但我的要求是从字符串集中搜索
-
但它不起作用不是一个足够好的解释
-
你能把你的文件内容贴出来吗?这将有很大帮助。
标签: java string java.util.scanner java-io