扫描仪只搜索 .txt 文件的第一行
Scanner only searching first line of .txt file
我正在开始编程 class,并且似乎在搜索文本文件时遇到了重大问题。根据分配,我的代码应该做什么:
- 接受输入,在本例中为名称,并将该输入放入 .txt 文件
- 允许用户搜索名称或名称的一部分,以及 return 具有匹配文本的所有行。
我已完成作业的输入部分,即将完成检索部分,但我的代码仅搜索 .txt 文件的第一行。我能够打印出 .txt 文件的所有行,如果我在 .txt 文件的第 1 行中搜索名称,它将正确打印该行。当我搜索不在第 1 行的名称时,我的问题就来了。下面是我的代码:
System.out.println ("Would you like to retrieve names from your index? (YES/NO)");
try
{
retrieve=input.readLine();
}
catch (IOException E)
{
System.out.println(E);
}
}
if (choice == 2 && retrieve.equalsIgnoreCase("YES") || retrieve.equalsIgnoreCase("Y"))
{
while (retrieve2.equalsIgnoreCase("YES") || retrieve2.equalsIgnoreCase("Y"))
{
FileReader reader = new FileReader("Name_Index.txt");
BufferedReader bufferedReader = new BufferedReader(reader);
String line = bufferedReader.readLine();
System.out.println ("Enter a string of characters in which to search by or enter \"all names\" f$
search_term = gatherInput();
System.out.println("Search results include: ");
ArrayList<String> list = new ArrayList<String>();
Scanner inFile = new Scanner (new File("Name_Index.txt"));
inFile.useDelimiter(",");
while (inFile.hasNextLine())
{
list.add(inFile.nextLine());
}
Collections.sort(list);
if (search_term.equalsIgnoreCase("all names"))
{
for (String temp : list)
{
System.out.println(temp);
}
}
else if (line.toLowerCase().contains(search_term.toLowerCase()))
{
System.out.println(line);
bufferedReader.close();
}
System.out.println("End!");
System.out.println ("Would you like to retrieve names from your index? (YES/NO)");
try
{
retrieve2=input.readLine();
}
catch (IOException E)
{
System.out.println(E);
}
}
System.out.println("Thank you, come again!");
}
}
public static String gatherInput()
{
Scanner scan = new Scanner(System.in);
String user_input = scan.nextLine();
return user_input;
}
}
我尝试扩展 while (inFile.hasNextLine()) 循环以包含第二个 "if" 语句,但是这会为 "all names" 搜索 - 它 return 多次搜索整个列表(无论文件中有多少行)。我什至尝试在第二个 "if" 语句中创建另一个 while (inFile.hasNextLine()) 循环,结果没有区别。
在这一点上我很沮丧,因为我已经在这个代码上工作了一个多星期,并且在没有任何帮助的情况下检查了我为此作业的所有笔记和讲座录音。任何见解将不胜感激。
您只阅读了文件的 1 行
String line = bufferedReader.readLine();
为什么不读取所有行并将它们存储在列表中;
List<String> lines = new ArrayList<>();
String line = bufferedReader.readLine();
while(line != null){
lines.add(line);
line = bufferedReader.readLine();
}
bufferedReader.close();
然后打印所有包含子字符串 ignorecase 的行:
lines.stream().filter(l -> l.toLowerCase().contains(search_term.toLowerCase))
.forEach(s -> System.out.println(s));
您需要循环 readLine()
例如:
File f = new File(ruta);
if(!f.exists()) //Error
else {
@SuppressWarnings("resource")
BufferedReader br = new BufferedReader(new FileReader(f));
String line;
while ((line = br.readLine()) != null) {
//line = the next line
}
}
我正在开始编程 class,并且似乎在搜索文本文件时遇到了重大问题。根据分配,我的代码应该做什么:
- 接受输入,在本例中为名称,并将该输入放入 .txt 文件
- 允许用户搜索名称或名称的一部分,以及 return 具有匹配文本的所有行。
我已完成作业的输入部分,即将完成检索部分,但我的代码仅搜索 .txt 文件的第一行。我能够打印出 .txt 文件的所有行,如果我在 .txt 文件的第 1 行中搜索名称,它将正确打印该行。当我搜索不在第 1 行的名称时,我的问题就来了。下面是我的代码:
System.out.println ("Would you like to retrieve names from your index? (YES/NO)");
try
{
retrieve=input.readLine();
}
catch (IOException E)
{
System.out.println(E);
}
}
if (choice == 2 && retrieve.equalsIgnoreCase("YES") || retrieve.equalsIgnoreCase("Y"))
{
while (retrieve2.equalsIgnoreCase("YES") || retrieve2.equalsIgnoreCase("Y"))
{
FileReader reader = new FileReader("Name_Index.txt");
BufferedReader bufferedReader = new BufferedReader(reader);
String line = bufferedReader.readLine();
System.out.println ("Enter a string of characters in which to search by or enter \"all names\" f$
search_term = gatherInput();
System.out.println("Search results include: ");
ArrayList<String> list = new ArrayList<String>();
Scanner inFile = new Scanner (new File("Name_Index.txt"));
inFile.useDelimiter(",");
while (inFile.hasNextLine())
{
list.add(inFile.nextLine());
}
Collections.sort(list);
if (search_term.equalsIgnoreCase("all names"))
{
for (String temp : list)
{
System.out.println(temp);
}
}
else if (line.toLowerCase().contains(search_term.toLowerCase()))
{
System.out.println(line);
bufferedReader.close();
}
System.out.println("End!");
System.out.println ("Would you like to retrieve names from your index? (YES/NO)");
try
{
retrieve2=input.readLine();
}
catch (IOException E)
{
System.out.println(E);
}
}
System.out.println("Thank you, come again!");
}
}
public static String gatherInput()
{
Scanner scan = new Scanner(System.in);
String user_input = scan.nextLine();
return user_input;
}
}
我尝试扩展 while (inFile.hasNextLine()) 循环以包含第二个 "if" 语句,但是这会为 "all names" 搜索 - 它 return 多次搜索整个列表(无论文件中有多少行)。我什至尝试在第二个 "if" 语句中创建另一个 while (inFile.hasNextLine()) 循环,结果没有区别。
在这一点上我很沮丧,因为我已经在这个代码上工作了一个多星期,并且在没有任何帮助的情况下检查了我为此作业的所有笔记和讲座录音。任何见解将不胜感激。
您只阅读了文件的 1 行
String line = bufferedReader.readLine();
为什么不读取所有行并将它们存储在列表中;
List<String> lines = new ArrayList<>();
String line = bufferedReader.readLine();
while(line != null){
lines.add(line);
line = bufferedReader.readLine();
}
bufferedReader.close();
然后打印所有包含子字符串 ignorecase 的行:
lines.stream().filter(l -> l.toLowerCase().contains(search_term.toLowerCase))
.forEach(s -> System.out.println(s));
您需要循环 readLine()
例如:
File f = new File(ruta);
if(!f.exists()) //Error
else {
@SuppressWarnings("resource")
BufferedReader br = new BufferedReader(new FileReader(f));
String line;
while ((line = br.readLine()) != null) {
//line = the next line
}
}