Java 我能';t在控制台中显示带有行号的.txt文件的名称

Java 我能';t在控制台中显示带有行号的.txt文件的名称,java,while-loop,Java,While Loop,我创建了一个Names.txt文件,其中有4个名字:比尔、戴夫、迈克和吉姆 我可以输入文件名,也可以输入要搜索的名称,例如上面的dave,然后控制台应该返回“dave出现在Names.txt的第2行”。相反,它返回“dave不存在”,如果它不是四个名称中的一个,则这是正确的。我在下面的while循环中犯了什么错误 public class names { public static void main(String[] args) throws IOException {

我创建了一个Names.txt文件,其中有4个名字:比尔、戴夫、迈克和吉姆

我可以输入文件名,也可以输入要搜索的名称,例如上面的dave,然后控制台应该返回“dave出现在Names.txt的第2行”。相反,它返回“dave不存在”,如果它不是四个名称中的一个,则这是正确的。我在下面的while循环中犯了什么错误

public class names {
    public static void main(String[] args) throws IOException {
        String friendName;    // Friend's name      

        // Create a Scanner object for keyboard input.
        Scanner keyboard = new Scanner(System.in);

        // Get the filename.
        System.out.print("Enter the filename: ");
        String filename = keyboard.nextLine();

        // Open the file.
        File file = new File(filename);
        Scanner inputFile = new Scanner(file);

        // Get the name of a friend.
        System.out.print("Enter name to search: ");
        friendName = keyboard.nextLine().toLowerCase();

        int lineNumber = 1;         

        while (inputFile.hasNextLine()) {
            if ("friendName".equals(inputFile.nextLine().trim())) {
                // found
                String line = inputFile.nextLine();
                System.out.println("friendName" + " appears on line " + lineNumber + " of Names.txt");
                lineNumber++; 
                //break;
            } else {
                // not found
                System.out.println(friendName + " does not exist. ");
                break;
            }
        }

        // Close the file.
        inputFile.close();
    }
}

friendName
中删除引号,并使用从文件中读取的实际变量:

int lineNumber = 1;
booelan found = false;

while (inputFile.hasNextLine()) {
    String nextLine = inputFile.nextLine().trim();
    if (friendName.equals(nextLine)) {
        // found
        found = true;
        break;        // name is found, no point in searching any further
    }

    lineNumber++;     // always increment the line number
}

if (found) {
    System.out.println(friendName + " appears on line " + lineNumber + " of Names.txt");
}
else {
    System.out.println(friendName + " does not exist. ");
}

我还改变了您使用
扫描仪的方式。在原始代码中,如果与朋友名匹配,则调用了两次
Scanner.nextLine()
。这将导致扫描仪前进两行,这不是您想要的。

为什么要比较字符串
“friendName”
,而不是字符串变量friendName。@Jackwhyski请查看给出的答案,谢谢。解决了您提供的程序中存在的问题,现在使用上面发布的示例程序进行尝试。更正了计数器逻辑中的缺陷。。一个观察结果是,当前程序搜索的名称区分大小写。
package com.stackoverflow;

import java.io.*;
import java.util.*;

public class Names
{
    public static void main(String[]args) throws IOException
    {
        String friendName;    // Friend's name

        // Create a Scanner object for keyboard input.
        Scanner keyboard = new Scanner(System.in);

        // Get the filename.
        System.out.print("Enter the filename: ");
        String filename = keyboard.nextLine();

        // Open the file.
        File file = new File(filename);
        Scanner inputFile = new Scanner(file);

        // Get the name of a friend.
        System.out.print("Enter name to search: ");
        friendName = keyboard.nextLine().toLowerCase();

        int lineNumber = 0;
        Boolean isFound = false;
        while(inputFile.hasNextLine()){
            lineNumber++;
            String line = inputFile.nextLine();
            if(line.trim().contains(friendName)){
                System.out.println(friendName + " appears on line " + lineNumber + " of Names.txt");
            }
        }
        if(!isFound){
            System.out.println("given friend name "+friendName+" not exists in the file");
        }
        // Close the file.
        inputFile.close();
    } }