gpt4 book ai didi

Java-在文本文件中搜索字符串,然后打印行

转载 作者:行者123 更新时间:2023-11-30 08:06:22 25 4
gpt4 key购买 nike

我正在编写一个代码,用户可以在文本文件中搜索特定字符串(不区分大小写),并打印找到该字符串的整行。我的代码片段在这里:

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/

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