Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我的try-catch在我的代码中不起作用?_Java_Debugging - Fatal编程技术网

Java 为什么我的try-catch在我的代码中不起作用?

Java 为什么我的try-catch在我的代码中不起作用?,java,debugging,Java,Debugging,我正在尝试创建一个代码来搜索列表中的给定名称。之后,它应该打印“已找到”或“未找到”。如果用户输入的文件不正确,代码应打印“读取文件”+文件+“失败”。但是,即使找到名称,它也会打印错误消息。我猜这是语法错误,但我花了45分钟试图找到它。这是我的密码: import java.nio.file.Paths; import java.util.Scanner; public class IsItInTheFile { public static void main(String[] a

我正在尝试创建一个代码来搜索列表中的给定名称。之后,它应该打印“已找到”或“未找到”。如果用户输入的文件不正确,代码应打印“读取文件”+文件+“失败”。但是,即使找到名称,它也会打印错误消息。我猜这是语法错误,但我花了45分钟试图找到它。这是我的密码:

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.");
        }

    }
}

问题出在内部while循环中。您正在从文件中读取行,而不检查是否还有更多行要读取。当您到达文件末尾时,它将抛出一个异常

重新构造代码,以便只有一个while循环,并在其中只调用一次
fhandle.nextLine()

        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;
                    }
                }
            }
        }

为了防止将来发生这种情况,请记住在catch子句中始终使用最具体的异常类型,例如
catch(IOException e)
。此外,当您捕获异常时,您应该始终以某种方式记录它,而不仅仅是假设您知道它是什么以及抛出它的原因。

FYI:使用catch块中的
e.printStackTrace()
打印异常跟踪,以查看错误是什么以及发生在哪里。不是你想象的那样。我看起来像是你删除了我放在循环外的“line=fhandle.nextLine();”。但是,如果没有这一行,代码将永远继续运行,因为它永远不会转到下一行。我如何修复它?你可以通过去掉内部while循环来修复它。只使用一个while循环,如答案中的代码所示