Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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 为什么字符串数组中的第一个字符串被忽略_Java_Arrays_String_Bufferedreader - Fatal编程技术网

Java 为什么字符串数组中的第一个字符串被忽略

Java 为什么字符串数组中的第一个字符串被忽略,java,arrays,string,bufferedreader,Java,Arrays,String,Bufferedreader,我写了一个程序,它的行为不符合预期。下面的代码应该从文本文件和数组中找到相等的字符串,并将其显示在控制台中 数组: public static List<String> words = new ArrayList<String>(); static { words.add("should"); words.add("find"); words.add("matches"); } 预期产出: fin

我写了一个程序,它的行为不符合预期。下面的代码应该从文本文件和数组中找到相等的字符串,并将其显示在控制台中

数组:

public static List<String> words = new ArrayList<String>();

    static
    {
        words.add("should");
        words.add("find");
        words.add("matches");
    }
预期产出:

find matches
实际产出:

matches

为什么呢?提前谢谢。

这对我有用。你试过跨过它吗?问题可能是你如何读取文件,或者文件本身。是的,我试过了。虽然首先需要“find”并尝试将其与“find”进行比较,但此比较的结果是错误的:(只是一些建议。在拆分后的字符串上使用trim()。在读取文件时使用trywithresource或确保正在关闭文件。是否可以尝试比较like
cand.trim().equals(cand1)
?@Kurlik-Kurlikovi4如果返回false,肯定有一些不同。你能将文件粘贴到你的问题中吗?它对我有用。你是否尝试过单步执行它?问题可能是你如何读取文件,或者文件本身。是的,我尝试过。尽管首先需要“查找”并尝试将其与“查找”进行比较,此比较的结果是错误的:(只是一些建议。在拆分后对字符串使用trim()。在读取文件时使用trywithresource或确保正在关闭文件。是否可以尝试进行类似
cand.trim().equals(cand1)的比较
?@Kurlik-Kurlikovi4如果返回false,则肯定存在一些差异。您能将该文件粘贴到您的问题中吗?
matches