-1

I'm trying to create a code that searches for a given name on the list. Afterwards, it should print "found" or "not found". If the user enters an incorrect file, the code should print, "Reading the file " + file + " failed.". However, it prints the error message, even when the name IS found. I'm guessing its a syntax error, but I spent 45 minutes trying to find it. Here is my code:

import java.nio.file.Paths;
import java.util.Scanner;

public class IsItInTheFile {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("Name of the file:");
        String file = scanner.nextLine();

        System.out.println("Search for:");
        String searchedFor = scanner.nextLine();
        boolean value=false;
        
        try(Scanner fhandle=new Scanner(Paths.get(file))){
            while(fhandle.hasNextLine()){
             
                String line = fhandle.nextLine();
                while(!(line.isEmpty())){
                    //System.out.println("Current line is "+line);
                    
                    String[] words=line.split(" ");
                    for(String word:words){
                        if (word.equals(searchedFor)){
                            
                            value=true;
                            //System.out.println(word);
                            
                            
                        
                        }
                        
                    }
                    line=fhandle.nextLine(); 
                }
               
            }
            
        }catch(Exception e){
            System.out.println("Reading the file " + file + " failed.");
            
        }
        if(value){
            System.out.println("Found!");
        }else{
            System.out.println("Not found.");
        }

    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Justin
  • 67
  • 6
  • 1
    *FYI:* [You should not catch `Exception` but the more specific type.](https://stackoverflow.com/questions/2416316/why-is-the-catchexception-almost-always-a-bad-idea) – akuzminykh Aug 08 '20 at 11:48
  • 1
    Print the exception trace with `e.printStackTrace()` in the catch block to see what the error is and where it happened. It's not what you think it is. – Joni Aug 08 '20 at 11:48

1 Answers1

1

The problem is in the inner while-loop. You are reading lines from the file, without checking if there are more lines to read. When you get to the end of the file, it will throw an exception.

Restructure your code so that there is only one while-loop, and call fhandle.nextLine() only once within it.

        while (fhandle.hasNextLine()) {
            String line = fhandle.nextLine();
            if (!line.isEmpty()) {
                String[] words = line.split(" ");
                for (String word : words) {
                    if (word.equals(searchedFor)) {
                        value = true;
                        break;
                    }
                }
            }
        }

To prevent this from happening in the future, remember to always use the most specific exception type in the catch clause, for example catch (IOException e) here. Also, when you catch an exception, you should always log it some how, and not just assume you know what it was and why it was thrown.

Joni
  • 108,737
  • 14
  • 143
  • 193
  • I looked like you have deleted the " line = fhandle.nextLine();" that I have placed outside the loop. However, without that line, the code will continue to run forever because it would never go to the next line. How do I fix that? – Justin Aug 08 '20 at 12:44
  • You fix it by getting rid of the inner while loop. Use only one while-loop, like the code in the answer shows – Joni Aug 08 '20 at 12:46