1

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?

1
  • And from Log Horizon xD Glad you noticed Commented Mar 8, 2015 at 6:57

4 Answers 4

4

Use a boolean to remember whether you have found a match, and display "Does not exist" only after checking every item and only if you have not found a match.

Sign up to request clarification or add additional context in comments.

Comments

2

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();

1 Comment

I'm still not familiar with Java so I don't know most of the terms like HashSet :( Thanks though :)
1

You're breaking 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();

1 Comment

Ooooh I see now. It acts like a on/off switch. Thank you
0
    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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.