【问题标题】:Why is my PrintWriter class not working as expected?为什么我的 PrintWriter 类没有按预期工作?
【发布时间】:2014-10-18 07:11:54
【问题描述】:

我有这个应用程序,它提示用户输入一个文本文件,从这个文本文件中,它包含整数和文本字符串。从那里,它应该写入另一个文本文件,result.txt。现在,由于我还是 IO 的新手,尽管文件已成功创建,但我在写入文件时遇到了问题。应用程序在用户输入文本文件名称后立即停止。那么你们能给我一些帮助吗?提前致谢!

import java.util.*;
import java.io.*;

class FileReadingExercise3 {

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

    String a = null;
    int sum = 0;

    do
    {
        try
        {
            System.out.println("Please enter the name of a file or type QUIT to finish");
            a = userInput.nextLine();

            if(a.equals("QUIT"))
            {
                System.exit(0);
            }

            fileInput = new Scanner(new File(a));
        }
        catch(FileNotFoundException e)
        {
            System.out.println("Error " + a + " does not exist.");
        }
    }while(fileInput == null);

    PrintWriter output = null;

    try
    {
        output = new PrintWriter(new File("result.txt"));
    }
    catch(IOException g)
    {
        System.out.println("Error");
        System.exit(0);
    }

    while(fileInput.hasNext())
    {
        if(fileInput.hasNextInt())
        {
            int num = fileInput.nextInt();
            sum += num;

            String str = Integer.toString(num);

            output.println(str);

        }
    }

    fileInput.close();
    output.close();
}
}

【问题讨论】:

  • a) 你为什么要两次new File("result.txt"));? b) 你的while(fileInput.hasNext()) 后面跟着if(fileInput.hasNextInt()) 看起来很狡猾。如果 while 为真但 if 为假,则无限循环?
  • @ScaryWombat 哦,对不起,那是试错,刚才忘了删除它。 erm 不知何故,我对我的 while 循环有点困惑,就像你提到的那样,哈哈。
  • 但是你的错误到底是什么?
  • @ScaryWombat erm 整个东西编译没有错误,但是运行时,在我输入要读取的 txt 文件后它就卡住了。

标签: java exception io printwriter


【解决方案1】:

因为你必须在调用hasNext()之后调用next()方法所以指针会转到输入文件的下一行。

另外你没有使用sum所以检查你是否需要这个变量。

这是有效的代码:

public static void main(String[] args) throws FileNotFoundException {
        Scanner userInput = new Scanner(System.in);
        Scanner fileInput = null;
        String a = null;
        int sum = 0;
        do {
            try {
                System.out
                        .println("Please enter the name of a file or type QUIT to finish");
                a = userInput.nextLine();
                if (a.equals("QUIT")) {
                    System.exit(0);
                }
                fileInput = new Scanner(new File(a));
            } catch (FileNotFoundException e) {
                System.out.println("Error " + a + " does not exist.");
            }
        } while (fileInput == null);

        PrintWriter output = null;
        try {
            output = new PrintWriter(new File("result.txt"));
        } catch (IOException g) {
            System.out.println("Error");
            System.exit(0);
        }
        while (fileInput.hasNext()) {
            if (fileInput.hasNextInt()) {
                int num = fileInput.nextInt();
                sum += num;
                String str = Integer.toString(num);
                output.println(str);
            } else {
                fileInput.next();
            }
        }
        fileInput.close();
        output.close();
    }
}

更新

根据 Scanner.hasNext() 方法的 java 文档:

如果此扫描器的输入中有另一个令牌,则返回 true。这 方法在等待输入扫描时可能会阻塞。 扫描仪不 超越任何输入。

所以要进入下一个位置,需要调用next()方法,否则Scanner会在同一个位置,程序陷入死循环。

【讨论】:

  • 嗨,谢谢您的帮助,现在它可以工作了,但是我总是不明白,那之后我们为什么要转到next()?哦,是的,我知道sum 变量未使用,但之后我会使用它,谢谢你提醒我。 :)
  • 哦,非常感谢您提供的信息,但在阅读Scanner.next() 时,它还说该方法可能会在等待输入扫描时阻塞,哈哈。这让我很困惑。
  • 假设如果你使用Scanner来读取用户输入的数据,那么代码看起来像这样Scanner s = new Scanner(System.in);String data = s.next();所以这里扫描器会等到用户输入一些数据并点击回车,所以在这种情况下,扫描仪被阻止输入。
  • 我明白了……我能帮个忙吗?您认为您可以看看我刚刚发布的与此应用程序相关的其他问题吗?再次感谢队友! stackoverflow.com/questions/25485260/…
猜你喜欢
  • 1970-01-01
  • 2020-02-29
  • 2019-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-04
  • 2021-01-03
相关资源
最近更新 更多