java 使用 readline 嵌套 while 循环

发布于 2024-08-08 22:19:16 字数 977 浏览 1 评论 0原文

我很困惑。我试图循环遍历两个文件,查看第一个文件每一行中的第一个标记,并将其与第二个文件每一行中的第三个标记进行比较。以下是嵌套 while 循环形式的逻辑结构:

BufferedReader reader1 = new BufferedReader(new InputStreamReader(new FileInputStream(fromFile1)));

BufferedReader reader2 = new BufferedReader(new InputStreamReader(new FileInputStream(fromFile2),"EUC-JP"));

String line1, line2 = null;
String temp1, temp2 = null;

while ((line1=reader1.readLine()) != null)
{
    StringTokenizer st1 = new StringTokenizer(line1);
    temp1 = "U"+st1.nextToken();
    while((line2=reader2.readLine()) != null)
    {
        StringTokenizer st2 = new StringTokenizer(line2);
        temp2 = st2.nextToken();
        temp2 = st2.nextToken();
        temp2 = st2.nextToken();
        if(temp2.equals(temp1));
        {
            System.out.println(temp1+" "+temp2);
        }
    }
}

但是,我在输出中看到的只是第一个文件第一行的第一个标记和第二个文件每一行的第三个标记重复了 6,000(长度文件 2) 次,无论它们是否“相等”。这与它们的不同编码有关吗?我可以看到这对 equals 测试有影响,但为什么循环行为不正确?

干杯, 布兰登

I'm confused. I'm trying to loop though 2 files looking at the first token in every line of the first file and comparing it to the third token of every line of the second file. Here is the logical structure in the form of a nested while loop:

BufferedReader reader1 = new BufferedReader(new InputStreamReader(new FileInputStream(fromFile1)));

BufferedReader reader2 = new BufferedReader(new InputStreamReader(new FileInputStream(fromFile2),"EUC-JP"));

String line1, line2 = null;
String temp1, temp2 = null;

while ((line1=reader1.readLine()) != null)
{
    StringTokenizer st1 = new StringTokenizer(line1);
    temp1 = "U"+st1.nextToken();
    while((line2=reader2.readLine()) != null)
    {
        StringTokenizer st2 = new StringTokenizer(line2);
        temp2 = st2.nextToken();
        temp2 = st2.nextToken();
        temp2 = st2.nextToken();
        if(temp2.equals(temp1));
        {
            System.out.println(temp1+" "+temp2);
        }
    }
}

However, all I see in the output is the first token from the first line of the first file and the third token from every line of the second file repeated 6,000 (the length of file 2) times regardless of whether they were "equal" or not. Does this have to do with their different encodings? I can see that having an effect on the equals test, but why isn't the loop behaving properly?

Cheers,
Brandon

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪之魂 2024-08-15 22:19:16

这是;但

if(temp2.equals(temp1));

它可能不会按预期工作,因为您必须在外循环中重新打开文件 2,否则它只能对文件 1 的第一行正常工作

it's the ; behind the if

if(temp2.equals(temp1));

But it wouldn't probably work anyway as expected, since you must reopen file 2 within the outer loop, otherwise it will only work correctly for the first line of file 1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文