我正在编写一个代码,用户可以在文本文件中搜索特定字符串(不区分大小写),并打印找到该字符串的整行。我的代码片段在这里:
public static void search() throws FileNotFoundException
{
Scanner input = new Scanner( System.in );
int fieldNumber = 0;
if ( fieldNumber == 0 )
{
System.out.println("Enter field value to search for: ");
String fieldValue = input.next();
if ("orange".equalsIgnoreCase(fieldValue))
{
// Here is where I am stuck.
{
}
最佳答案
try
{
Scanner file = new Scanner(new File("file.txt"));
String str = str.nextLine();
System.out.print("Enter search ");
Num = input.nextInt();
if ( Num == 0 )
{
System.out.println("Enter search for: ");
String Value = input.next();
while (file.hasNextLine())
{
final String lineFromFile = file.nextLine();
if(lineFromFile.contains(Value))
{
System.out.println("file.txt");
System.out.println(lineFromFile);
break;
}
关于Java-在文本文件中搜索字符串,然后打印行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31039766/