简体   繁体   English

Java从文本(txt)文件中读取字符串

[英]Java reading a string from a text(txt) file

Rod 竿
Rae
Bryan 布赖恩
Shiroe 白荣
Ric 里克
Kirito 桐人
Asuna 明日奈
Elsa 艾尔莎
Akutabe uta部
Shino 筱野

I have that list saved in a text file. 我已将该列表保存在文本文件中。 If I were to enter Rod, it should say "Exists" and if I enter a name that is not on the list, it should say "Does not exist." 如果要输入Rod,则应输入“ Exists”,如果输入的名称不在列表中,则应输入“不存在”。 But what is happening on my code is that it reads the file per line and prints "Does not exist" if it does not match the string line. 但是在我的代码上发生的事情是,它每行读取一个文件如果与字符串行不匹配,则显示“不存在”。 So if I were to enter a name that does not exist in the txt file, it would print 10 "Does not exist" lines. 因此,如果我输入的是txt文件中不存在的名称,它将打印10行“不存在”。

This is my code below: 这是我的代码如下:

Scanner in = new Scanner(System.in);
    out.print("Enter name: ");
    String name = in.nextLine();

    BufferedReader br = new BufferedReader(new FileReader("name.txt"));
    String line;
    while ((line = br.readLine()) != null) {
        if (line.contains(name)) {
            out.println("Exists");
            break;
        } else {
            out.println("Does not exist");
        }
    }
    br.close();

An example of a what would be output is: 输出的示例如下:

name = Kirito 名字= Kirito

Does not exist 不存在
Does not exist 不存在
Does not exist 不存在
Does not exist 不存在
Exists 存在

Why does my program print so many Does not exist before finding the exact match? 为什么我的程序打印出这么多在找到完全匹配之前Does not exist

使用boolean来记住您是否找到了匹配项,并且仅在检查每个项目之后并且仅当您没有找到匹配项时才显示“不存在”。

You were almost there. 你快到了 You are just preemptively printing the error message. 您只是抢先打印错误消息。 I would have also used equals instead of contains and pre-loaded the entire file into. 我也将使用equals而不是contains并将整个文件预加载到其中。 HashSet if multiple queries need to be answered HashSet如果需要回答多个查询

Scanner in = new Scanner(System.in);
out.print("Enter name: ");
String name = in.nextLine();

BufferedReader br = new BufferedReader(new FileReader("name.txt"));
String line;
boolean found = false;
while ((line = br.readLine()) != null) {
    if (line.contains(name)) {
        out.println("Exists");
        found = true;
        break;
    }
}
if (!found) {
             out.println("Does not exist");
}
br.close();

You're break ing the loop if the name exists, so you should only print the "not exists" message if the loop doesn't break: 如果名称存在,则要break循环,因此,如果循环中断,则只应打印“不存在”消息:

Scanner in = new Scanner(System.in);
out.print("Enter name: ");
String name = in.nextLine();

BufferedReader br = new BufferedReader(new FileReader("name.txt"));
String line;
boolean nameFound = false;
while ((line = br.readLine()) != null) {
    if (line.contains(name)) {
        out.println("Exists");
        nameFound = true;
        break;
}
if (!nameFound) {
    out.println("Does not exist");
}
br.close();
    PrintStream out = System.out;
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    out.print("Enter name: ");
    String name = in.readLine();

    BufferedReader br = new BufferedReader(new FileReader("name.txt"));
    String line;
    boolean ifexist = false;
    while ((line = br.readLine()) != null) {
        if (line.contains(name)) {
            ifexist = true;
            break;
        }
    }
    if (ifexist) {
        out.print("Exist");
    } else {
        out.println("Does not exist");
    }
    br.close();

Add a boolean var default false, when exist set it to true and break. 添加一个布尔型默认值false,如果存在则将其设置为true并中断。 Than output. 比输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM