如何正确在Java中获取包含特定单词的每一行。

huangapple go评论71阅读模式
英文:

how to properly get every line containing a specific word in java

问题

System.out.println("搜索图书");

String search = scan.next();
scan.nextLine();

File file = new File("library.txt");
Scanner in = null;
in = new Scanner(file);

String line = in.nextLine();
while(in.hasNext()) {
    if(line.contains(search)) {
        System.out.println(line);
    }

    if(!line.contains(search)) {
        System.out.println("未找到匹配结果");
        System.exit(0);
    }
}
英文:

I'm trying to write a program that reads a file and checks every line that contains a specific word then prints it.if there isn't one it should print "no match for your search".this is what I've got so far and and I'm having trouble putting it all together.after all my twinkering around and replacing thewhile with ifor putting the second if statement outside of the while, sometimes it doesn't matter what i enter it always says "no match for your search" and sometimes it says java.util.NoSuchElementException: No line found. and sometimes it freezes and i searched this up and it says its a bug in cmd or something.any help will be appreciated
and I'm new to java so please any thing you can advise me will be helpful and appreciated

System.out.println("search for book");
 
String search = scan.next();	
scan.nextLine();	

File file = new File("library.txt");
Scanner in = null;
in = new Scanner(file);
          
String line = in.nextLine();
while(in.hasNext()) {
    if(line.contains(search)) {
	    System.out.println(line);
	} 	
		   
    if(!line.contains(search)) {
		System.out.println("no match for your search");
		System.exit(0);
	}
}

答案1

得分: 2

不提及您代码中的逻辑错误,您应该在循环外创建一个逻辑(布尔)变量,并将其设置为“false”。如果遇到您的条件,请将其设置为“true”。
在while循环之后,检查这个值。如果它为false,这意味着没有找到任何行,您应该打印您的消息。

示例:

boolean foundAnything = false;
while (...) {
    ...
    if (condition) {
        foundAnything = true;
        ...
    }
    ...
}

// 没有找到任何内容
if (!foundAnything) {
    ...
}
英文:

Not mentioning logical errors in your code, you should probably create logical (boolean) variable outside of the loop and set it to false. If you encounter your condition, set it to true.
After the while loop, check the value. If it's false, that means no lines were found and you should print your message.

Example:

boolean foundAnything = false;
while(...) {
    ...
    if(condition) {
        foundAnything = true;
        ...
    }
    ...
}

// Nothing was found
if(!foundAnything) {
    ...
}

答案2

得分: 1

> 有时候不管我输入什么,它总是显示 "没有符合您搜索条件的结果"

在这里,您的循环内部存在最大的问题:

while(in.hasNext()) {
    if(line.contains(search)) {
        System.out.println(line);
    }   
           
    if(!line.contains(search)) {
        System.out.println("没有符合您搜索条件的结果");
        //这里!!!
        System.exit(0);
    }
}

System.exit(0) 会终止程序,不会执行其他任何代码。因此,如果在行中未找到 search 单词,程序将会结束。

> 有时候会显示 java.util.NoSuchElementException: 找不到行

您在循环之前读取了第一行,并且可能文件是空的。

File file = new File("library.txt");
Scanner in = null;
in = new Scanner(file);

//这行代码读取了文件的第一行
String line = in.nextLine();
while(//代码的其余部分...

您可以通过以下方式解决这两个问题:

  • 仅在循环中读取文件的内容
  • 使用标志来检查是否找到了目标单词
  • 仅在找到单词或文件没有更多行时才停止循环
  • 在循环中,如果目标单词尚未找到,就让循环继续
  • 除非绝对必要,避免使用 System#exit
  • 如果循环结束后仍未找到目标单词,打印一条消息

根据这些建议,您的代码可以设计如下:

File file = new File("library.txt");
Scanner in = new Scanner(file);

//使用标志来检查是否找到了目标单词
boolean found = false;

//仅在找到单词或文件没有更多行时才停止循环
while (!found && in.hasNextLine()) {
    //仅在循环中读取文件的内容
    String line = in.nextLine();
    if (line.contains(search)) {
        found = true;
        System.out.println(line);
    }
    //在循环中,如果目标单词尚未找到,就让循环继续
}
//如果循环结束后仍未找到目标单词,打印一条消息
if (!found) {
    System.out.println("没有符合您搜索条件的结果");
}
英文:

> sometimes it doesn't matter what i enter it always says "no match for your search"

The biggest issue here is this part inside your loop:

while(in.hasNext()) {
    if(line.contains(search)) {
        System.out.println(line);
    }   
           
    if(!line.contains(search)) {
        System.out.println("no match for your search");
        //HERE!!!
        System.exit(0);
    }
}

System.exit(0) will stop the program and nothing else will be executed. So if the search word is not found in the line, the program finishes.

> sometimes it says java.util.NoSuchElementException: No line found

You read the first line before the loop and maybe you have an empty file.

File file = new File("library.txt");
Scanner in = null;
in = new Scanner(file);

//this reads the first line of the file
String line = in.nextLine();
while(//rest of code...

You can overcome these two issues by:

  • Read the contents of the file only in the loop
  • Use a flag to check if the word was found
  • Stop the loop only if the word was found OR if the file has no more lines
  • In the loop, if the word is not found yet, just let it continue
  • Avoid using System#exit unless really needed
  • If after the loop the word was not found, print a message

With these suggestions in mind, your code can be designed like this:

File file = new File("library.txt");
Scanner in = new Scanner(file);

//Use a flag to check if the word was found 
boolean found = false;

//Stop the loop only if the word was found OR if the file has no more lines
while (!found && in.hasNextLine()) {
    //Read the contents of the file only in the loop
    String line = in.nextLine();
    if (line.contains(search)) {
        found = true;
        System.out.println(line);
    }
    //In the loop, if the word is not found yet, just let it continue
}
//If after the loop the word was not found, print a message
if (!found) {
    System.out.println("no match for your search");
}

答案3

得分: 0

首先,您似乎跳过了第一行。其次,第二个if条件是多余的。

Boolean found = false;
while (in.hasNext()) {
    String line = in.nextLine();
    if (line.contains(search)) {
        System.out.println(line);
        found = true;
    }
}

if (found == false) System.out.println("没有找到与您的搜索匹配的结果");
英文:

First of all you seem to skip your first line. Secondly, second if clause is redundant.

Boolean found=false;
while(in.hasNext()) {
    String line = in.nextLine();
    if(line.contains(search)) {
        System.out.println(line);
        found=true;
    }           
}

if(found==false) System.out.println("no match for your search");

huangapple
  • 本文由 发表于 2020年9月4日 02:01:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63729296.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定