简体   繁体   中英

Java reading a string from a text(txt) file

Rod
Rae
Bryan
Shiroe
Ric
Kirito
Asuna
Elsa
Akutabe
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." 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.

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

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?

使用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. HashSet if multiple queries need to be answered

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:

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. Than output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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