簡體   English   中英

為什么我的PrintWriter類無法正常工作?

[英]Why is my PrintWriter class not working as expected?

我有這個應用程序,它提示用戶輸入一個文本文件,該文本文件包含整數字符串和文本。 然后應該從那里寫入另一個文本文件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();
}
}

因為你要調用它被卡住next()調用之后方法hasNext()使指針轉到您的輸入文件的下一行。

另外,您沒有使用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()方法,否則掃描程序將位於相同位置,並且程序陷入無限循環。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM